rerun
rerun

Reputation: 25505

vs2010 C4353 why isn't this an error

I ran into this today in an if and after looking into it found that all these are all valid statements that generate the C4353 . My only guess is that this is the old way of doing noop in C. Why is this not an error. When would you use this to do anything useful.

int main()
{
    nullptr();
    0();
    (1 == 2)();
    return 0;

}

Upvotes: 1

Views: 353

Answers (4)

sehe
sehe

Reputation: 393934

The MSDN page for that warning has ample explanation and a motivating example:

// C4353.cpp
// compile with: /W1
void MyPrintf(void){};
#define X 0
#if X
   #define DBPRINT MyPrint
#else
   #define DBPRINT 0   // C4353 expected
#endif
int main(){
    DBPRINT();
}

As you can see it is to support archaic macro usage.

Upvotes: 0

Frédéric Hamidi
Frédéric Hamidi

Reputation: 263157

As explained in the C4353 warning page and in the __noop intrinsic documentation, the use of 0 as a function expression instructs the Microsoft C++ compiler to ignore calls to the function but still generate code that evaluates its arguments (for side effects).

The example given is a trace macro that gets #defined either to __noop or to a print function, depending on the value of the DEBUG preprocessor symbol:

#if DEBUG
   #define PRINT   printf_s
#else
   #define PRINT   __noop
#endif

int main() {
   PRINT("\nhello\n");
}

Upvotes: 1

BЈовић
BЈовић

Reputation: 64293

All of these :

    nullptr();
    0();
    (1 == 2)();

are no-op statements (meaning they don't do anything).

btw I hope you are not ignoring warnings. Most of the time it is a good practice to fix all warnings.

Upvotes: 1

Dabbler
Dabbler

Reputation: 9873

Using constant 0 as a function expression is an extension that is specific to Microsoft. They implemented this specifically because they saw a reason for it, which explains why it's wouldn't make sense to treat it as an error. But since it's non-standard, the compiler emits a warning.

You are correct that it is an alternative to using __noop().

Upvotes: 3

Related Questions