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)

[edit] Optional?

I find it confusing that the #define syntaxes are marked as (optional). In this case I think it just means the replacement-list can be left, but in <cstdint> it is used to denote optional features that the implementation does not need to support. Wouldn't it be good if these two different meanings were denoted in different ways so that it became more clear in what way something is optional? 213.67.240.59 04:55, 15 November 2017 (PST)

[edit] Split Predefined Macros

When I search for things like __cplusplus, the search box doesn't find it (google does). That brings me to this page describing macro replacement. I think the predefined macros should be placed on their own page and added to the language index. This would make them easier to browse to since search doesn't work. Also, it appears that many more predefined macros are coming in the future with the feature detection work that is currently proposed. -- Legalize (talk) 14:36, 24 February 2018 (PST)

I agree with this. The predefined macros for C and C++ should be split out and __STDC__, __FILE__, __DATE__, __TIME__, __LINE__ should be searchable on in the search box. Currently nothing is found. Any other predefined macros mentioned by ISO C and ISO C++ standards should be mentioned. StephenHowe (talk) 03:01, 14 November 2019 (PST)

I agree, too (I was going to add a comment to the same effect, and found yours). IMHO "Language feature-testing macros" should be in its own page, as well. Both pages could be linked to from here. Gennaro Prota (talk) 11:52, 2 February 2020 (PST)