Namespaces
Variants
Views
Actions

Talk:cpp/preprocessor/replace

From cppreference.com

Perhaps this quirk would be worth mentioning?

#include <cassert>
#include <type_traits>
using namespace std;
 
int main()
{
    assert(is_same<int, int>::value);
}
 
prog.cpp:7:33: error: macro "assert" passed 2 arguments, but takes just 1
  assert(is_same<int, int>::value);
                             ^
prog.cpp: In function 'int main()':
prog.cpp:7:2: error: 'assert' was not declared in this scope
  assert(is_same<int, int>::value);
  ^


It seems the issue is not only with assert macro, but actually with all preprocessor macros: http://stackoverflow.com/questions/40598231/why-cant-i-use-assert-with-stdis-same

It needs to be clarified if:

1) macros are not guaranted to work at all with code with templates (is it UB? ill-formed? unspecified whether works or not?) or 2) macros are guaranteed to work with such code provided their arguments are enclosed with an extra pair of ()s - assert((is_same<int, int>::value)) actually works

And also: what are any other constructs that may be incompatibile with macros (from SO: "C code has the same problem: the preprocessor only recognizes parentheses. assert(a[1, 1]); is an error, likewise for compound literals. It's just far more pronounced in C++.")

sure, commas in function-like macro arguments are a common problem and worth a paragraph in the Notes section. I remember running into this when using boost.foreach (and their docs explain it here). Added a small note here and in assert --Cubbi (talk) 12:40, 17 November 2016 (PST)