2

I am getting the following error message

error: '0' cannot be used as a function

when trying to compile the following line:

NOOP(0 != width);

NOOP is defined as follows:

#define NOOP (void)0

The source code is part of a SDK - so it should be okay. And I have found out that (void)0 actually is a valid way to descibe "no operation" in C++. But why would you want to pass a boolean parameter to a function which does nothing? And how do you get rid of the error message?

Aoshi
  • 53
  • 4
  • You can't get rid of the error message because it's an error. It's not an arbitrary thing you can turn off. It gives an error because _it cannot proceed_. – Mooing Duck Nov 06 '12 at 00:09
  • @MooingDuck: It's reasonably safe to assume that when people ask about getting rid of an error, an answer that would solve the underlying problem is what they're after, instead of hairsplitting over semantics. – millimoose Nov 06 '12 at 00:10
  • 1
    I'm oddly reminded of [`Acme::Don't`](http://search.cpan.org/~dconway/Acme-Don-t-1.01/t.pm) – millimoose Nov 06 '12 at 00:11
  • 1
    @millimoose: That's why I posted an answer to the error, and left the semantics in a comment. – Mooing Duck Nov 06 '12 at 00:14
  • "In other words, doin nothing about doing nothing does...nothing." - uh. – Griwes Nov 06 '12 at 00:25
  • Can't you ask the provider og that SDK what this is about? Because without more information, this really looks like an error. Maybe a bug in the SDK (assuming that definition and use of NOOP are both part of the SDK)? – Sebastian Nov 06 '12 at 00:32

2 Answers2

5

The MACRO is not defined with any parameters on it, so after the preprocessor replaces code, that statement ends up looking like this:

(void)0(0 != width);

Which confuses the compiler into thinking you are trying to use the "()" operator on 0. (i.e. using 0 as a function)

I recommend that you drop the "(0 != width)" (it is misleading) and just write NOOP;

imreal
  • 10,178
  • 2
  • 32
  • 48
  • "I recommend that you drop the "(0 != width)" (it is misleading) and just write NOOP;" NOOP is used several times like this. So I think, there must be a sense behind it. Another example: NOOP(NULL != bitmap). – Aoshi Nov 06 '12 at 00:23
  • Is it possible that there are multiple definitions of this macro in your project? Maybe it is actually defined `#define NOOP(x) (void)0`? – leemes Nov 06 '12 at 00:26
  • @Aoshi very strange, it looks like a conditional noop, in which case it should use the expression somehow. Can you modify the SDK? Out of curiosity, what SDK is this? – imreal Nov 06 '12 at 00:34
  • The SDK is called Logitech LCD SDK. And yes, I can modify the SDK. So I will try Mooing Ducks suggestion and add an (X) behind NOOP. @leemes This is the only definition of the macro. – Aoshi Nov 06 '12 at 00:41
4

"(void)0(0!=width);" is not valid C++, so it's not OK. (void)0; by itself doesn't do anything in C++, so can be used as a noop. Instead of your current define, I would use:

#define NOOP(X) (void)0

This tells the C++ preprocessor that there is a preprocessor function called NOOP that takes one parameter of any type, and replaces that entire function call with (void)0. So if you have a line of code that says NOOP("HELLO WORLD"), then the preprocessor replaces that entire thing with (void)0, which the C++ compiler proceeds to ignore.

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
  • Okay, this would allow me to "get rid" of the error message. But I still do not understand, what it actually does? Could you explain it (in a few words), please? – Aoshi Nov 06 '12 at 00:27
  • @Aoshi: reworded my answer to clarify a little how the preprocessor works – Mooing Duck Nov 06 '12 at 00:37
  • Thank you for your answer. I will try out your suggestion and respond to your message tomorrow. – Aoshi Nov 06 '12 at 00:46