1

This is my assert function (it wont compile "error C2110: '+' : cannot add two pointers"):

#define CHAR(x) #x

template<typename T>
inline void ASSERT(T x)
{
    if(!x)
    {
        std::string s("ERROR! Assert " + CHAR(x) + " failed. In file " + __FILE__ +
                      " at line " + __LINE__ + ".");
            std::wstring temp(s.length(), L' ');
            std::copy(s.begin(), s.end(), temp.begin());
            getLogger().Write(temp);
        }
    }

Any idea of how to fix it?

5
  • 4
    Note that you can't write this as a function; the use of __FILE__ and __LINE__ require that it is implemented as a macro (assuming you want the file and line in which the ASSERT is used). Commented Sep 15, 2011 at 22:49
  • @James McNelles even if I make the function inline? Commented Sep 15, 2011 at 22:57
  • Yes, even then. Inlining doesn't affect the output of a function. (If it does, your compiler is broken.) Commented Sep 15, 2011 at 23:01
  • 1
    @Tiago Costa: It needs to be a macro because __FILE__ and __LINE__ are preprocessor macros which will be "magically" replaced by constants later. Commented Sep 15, 2011 at 23:04
  • When you're writing C++, you should use std::string. Then this entire issue just disintegrates. (This means that each [sort of] string literal in your code should be wrapped in a temporary instantiation of std::string; then you can rely on std::string's op+ to do the heavy lifting for you.) In this case, as others have said, you can just rely on preprocessor concatenation, but this won't always be available to you. Commented Sep 15, 2011 at 23:28

5 Answers 5

2

String Literals are easily reduced to char pointers, which cannot be added as you try to do with "ERROR! Assert " + CHAR(x) + " failed. In file ".... However, C++ has the handy feature of doing this automatically before compilation! (the preprocessor does this). Even better, it has a handy tool for making wide strings at compile time. So, you want:

#define _T(x) L ## x
#define CHAR(x) #x
#define CHAR2(x) CHAR(x)
#define ASSERT(x) ASSERT2(x, CHAR(x), __FILE__, CHAR2(__LINE__))
#define ASSERT2(x, t, f, l) \
if(!x) \
    getLogger().Write(L"ERROR! Assert " _T(t) L" failed. In file " _T(f) L" at line " _T(l) L".");

http://ideone.com/0ibcj

Sign up to request clarification or add additional context in comments.

6 Comments

No compile errors. But #define CHAR(x) #x is not working correctly... Its outputting: "ERROR! Assert x failed. In file FILE at line LINE."
Are FILE and LINE displaying properly? Hard to tell from your comment. Also, did you make ASSERT into a macro or a function?
No they are not displayed properly... I turned ASSERT into a macro
What happens if you remove the CHAR() macro and put in just # then?
I've changed the code to this: ideone.com/gBceP. But I get 3 compile errors: error C2064: term does not evaluate to a function taking 1 arguments / error C2143: syntax error : missing ')' before 'string' / error C2059: syntax error : ')'
|
0

The compiler error is quite clear; you are trying to apply the + operator to string literals. A quick way to fix it is enclosing the first string literal in std::string().

As @James McNellis pointed out, note that FILE and LINE will point to the file and line of the assert function declaration.

Comments

0

You cannot use the + operator to concatenate two char*s; you need printf or some sort of thing for that.

Comments

0

"ERROR! Assert " is a null-terminated, C-style string. You can't execute operator+ on it.

1 Comment

Sure you can. It just doesn't do what's desired here, and there are restrictions on what the other operand can be.
0

A few issues:

  1. Generally an assert should break into the debugger or dump if one is not attached. This will not.
  2. As already mentioned, your LINE and FILE require use in a macro
  3. You need a couple "helper" macros to get the strings working properly

Try something along these lines:

#define ASSERT_QUOTE_(x) #x
#define ASSERT_QUOTE_(x) ASSERT_QUOTE_(x)

#define MY_ASSERT(cond) \
  if(cond) {} else { \
    std::stringstream ss; \
    ss << "ERROR! Assert " << ASSERT_QUOTE(cond) << " failed. In file " << __FILE__ << " at line " << __LINE__ << "."; \
    getLogger().Write(ss.str()); \
  }

Be careful trying to use STL here however. I suggest you have your logger's Write() function take variable arguments and process them with printf() or perhaps boost::format

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.