jiafu
jiafu

Reputation: 6546

what's the "#define XXX" 's value?

what's the "#define XXX" 's value?

it has no value but it seems no compile error .normally the define is define type replace, but it is define type wiouht replace str.

Upvotes: 4

Views: 2576

Answers (3)

Pete Becker
Pete Becker

Reputation: 76235

As other have said, its definition is empty, that is, in most contexts its replacement text is empty. However, in the constant expression for a #if statement, its value is 0:

#define XXX
#if XXX == 0
    // yes, we get here
#elif
    // no, we don't get here
#endif

Same thing for a name that is not defined at all:

#if YYY == 0
    // yes, we get here
#elif
    // no, we don't get here
#endif

The difference between those two is that the latter is not defined, and the former is:

#ifdef XXX
    // yes, we get here
#endif

#if defined(XXX)
    // yes, we get here
#endif

and for YYY neither of those tests succeeds.

Upvotes: 3

Kerrek SB
Kerrek SB

Reputation: 476940

It'll replace the replacement text by nothing:

#define FOO

int FOO main() FOO
{
}

Moreover, #ifdef FOO will succeed.


Empty defines can be quite useful, for example in this (naive) functional form:

#ifndef NDEBUG
#  include <cstdlib>
#  define MakeSureThat(X) if (!(X)) { std::abort(); }
#else
#  define MakeSureThat(X)
#endif

Usage:

void do_stuff(Foo * p)
{
    MakeSureThat(p != nullptr);       // won't generate any code if NDEBUG
}

Upvotes: 11

RiaD
RiaD

Reputation: 47619

It will define XXX to the rest of the line, as usual.

It doesn't matter that it's empty in this case.

There are no any exceptions here.

Upvotes: 4

Related Questions