1

I am porting some C++ codebase from Windows VC10 to Mac XCode 10.7 .I have a macro which wraps the "assert()" .The Microsoft compiler has no problem with the following definition:

void assert(bool result, const char *call, const char *file, int line);
/// Wraps \c assert().//
#define MY_ASSERT(call) (mynsp::assert((call), #call, __FILE__, __LINE__))

while XCode throws me an error : Too many arguments provided to function-like macro invocation

Being complete noob to OS X and LLVM my question is how to work around this issue?

Btw, assert() declaration is wrapped with custom namespace (mynsp)

5
  • Works fine with G++. How many arguments do you pass to the macro? Commented Sep 3, 2013 at 11:36
  • 1
    Do you have <cassert> included somewhere? Commented Sep 3, 2013 at 11:37
  • No I don't have.Should I? Commented Sep 3, 2013 at 11:37
  • 1
    No, you shouldn't. Probably some other header does. This is one of the reasons why macros suck. Commented Sep 3, 2013 at 11:38
  • 1
    assert is the name of a macro defined in <cassert>; I suggest you use a different name for your function. Either that will solve your problem, or at least prevent WTF moments for people reading your code. When I see assert in code, I simply assume it's the standard one. Commented Sep 3, 2013 at 11:38

2 Answers 2

2

The standard header <cassert> (or <assert.h> in C) defines assert as a macro, making that name unusable for any other purpose. Even if you don't include that header yourself, it's possible that it's included indirectly from some other header. That is probably why you only see the problem on one platform, not both.

The best option is to rename your function to avoid the clash; alternatively, you could use #undef assert in any file that wants to use the name for your purpose.

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

1 Comment

Indeed,it was just a matter of changing the name.
1

Right-click on assert in your prototype and choose "Jump to Definition". You'll see that it's #define'd somewhere, causing this problem for you.

You could #undef assert to get past it.

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.