Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ macro without spaces

I need a macro to expand to a c++ comment, is that possible?

I've got this:

#define SLASH(x,y) x y
#define OUT SLASH(/,/)

int main(int argc, char *argv[])
{
  OUT << "text";
  return 0;
}

And need to expand to this:

{
  // << "text";
  return 0;
}

I've also tried this:

#define SLASH(x) /x
#define OUT SLASH(/)

But the result is still the same:

int main(int argc, char *argv[])
{
  / / << "text";
  return 0;
}
like image 312
Samuel Avatar asked Jan 17 '26 23:01

Samuel


1 Answers

No it's not possible because in C++ comments are removed before macros are expanded.

(See 2.1 of the standard, comment removal happens in phase 3, macro expansion in phase 4.)

like image 172
CB Bailey Avatar answered Jan 19 '26 14:01

CB Bailey