13

I am trying to create an exception class. For this I overloaded the << operator. So the code is something like this

class RunAndCheck
{
     opearator << (boost::any given)
     {

         //Here goes the value of the    "given"

     }
};

The usage is like this

RunAndCheck s;
s << file->open() << __FILE__ << __LINE__ ; 

So the problem is that I want to know the type of the FILE, then only I can extract the string out of boost::any. Can anybody invoke your curiosity around this?

3 Answers 3

11

__FILE__ expands into a string literal, just as if you had written "/path/to/current/file.cpp" directly. String literals are non-modifiable char array lvalues.

You want to template that << instead of using boost::any:

class RunAndCheck {
public:
    template<class T>
    RunAndCheck& operator<<(const T& value) {
        // ...
        return *this;
    }
};

Or you want to provide overloads for all the acceptable types:

class RunAndCheck {
public:
    RunAndCheck& operator<<(const char* value) {
        // ...
        return *this;
    }
    RunAndCheck& operator<<(int value) {
        // ...
        return *this;
    }
};
2
  • I have to use some if else conditions based on the type, like if(the input is no) cout << " line no " << no, if(is string) cout << " file name " << fileName. Can you say how to achieve that? Feb 21, 2011 at 11:07
  • @prabhakaran: Make sure you look at visitor's answer. Even though I resolved the syntax/type problem you asked about, the other is the correct semantic solution. Feb 21, 2011 at 13:03
8

Macros do not have types, they are just text replacements made by the preprocessor (without type checks). The type of the value dropped in by __FILE__ is a constant C string.

7
  • @jdehaan Can you explain me further. Is C String means CString or some other Feb 21, 2011 at 11:01
  • 3
    @Stephane Rolland: const char* is incorrect, though you can get that from a string literal. Feb 21, 2011 at 11:10
  • a C string is not a class. it is a const literal, exactly if you typed "c:\path\file.cpp" directly into the source code. The type of that string literal is const char[].
    – tenfour
    Feb 21, 2011 at 11:10
  • @tenfour: technically, its type is (equivalent to) char[] for historical reasons, though it should always be treated as if it were const.
    – Fred Foo
    Feb 21, 2011 at 11:35
  • 2
    Saying "macros do not have types" is less than useful. The value that a macro expands to has a type, just as all values do.
    – Jim Balter
    Feb 21, 2011 at 11:37
5

__FILE__ is replaced by a string literal, whose type is

const char[length_of_particular_string]

You should really reconsider what you are doing. (Opinion also based on your previous question.)

Firstly, boost::any is no good for such usage (in particular since the type of the string literal will be different in different cases). But even if not for the technical difficulties, you should use normal function overloading.

More importantly, it seems that the functionality that you want is to receive a boolean value and throw an error containing the file name and line number if the value is not true. Since you always require all 3 components (although according to your description it would be possible to have it throw without giving it the file name, or have the class do nothing useful), a function that takes those 3 arguments makes more sense.

Furthermore, you can now wrap calls to this in a macro, so that the file name and line number are automatically provided.

Full example:

#include <stdexcept>
#include <sstream>
#include <iostream>

void check_result(bool result, const char* line, int line_number)
{
    if (!result) {
        //for example:
        std::stringstream ss;
        ss << line << ' ' << line_number;
        throw std::runtime_error(ss.str()); 
    } 
} 

#define CALL_AND_CHECK(expression) check_result((expression), __FILE__, __LINE__)

bool foobar(bool b) { return b; }

int main()
{
    try {
        CALL_AND_CHECK(foobar(true));
        CALL_AND_CHECK(foobar(false));
    } catch (const std::exception& e) {
        std::cout << e.what() << '\n';
    }
}
2
  • Very good advice, but you might want to change __LINE__ to __FILE__. :) Feb 21, 2011 at 11:53
  • see that previous question's new edit ("stackoverflow.com/questions/5062699/…;). Mar 1, 2011 at 6:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.