Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

string

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.

like image 294
jiafu Avatar asked Dec 02 '22 19:12

jiafu


1 Answers

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
}
like image 69
Kerrek SB Avatar answered Dec 13 '22 08:12

Kerrek SB