Passing a template that requires a comma for a macro with one argument

I have a code that condenses substantially to

#define FOO(a)
FOO(std::map<int, int>);

But it emits a compilation error (there are too many actual parameters for the macro FOO).

Obviously, the preprocessor think that I have set std::map<int, and int>as arguments.

Is there any way around this? Therefore, the preprocessor will not process the quoted string with a comma.

+4
source share
4 answers

Ideally, it could be a comment, but SO doesn't support the code in the comments, so you can do

#include <map>

#define T_ARGS( ... ) < __VA_ARGS__ >

#define FOO( a )  a x;

auto main() -> int
{
    FOO( std::map T_ARGS( int, int ) );
    (void) x;
}

, , , (, ).

+2

, .

Boost, BOOST_PP_COMMA:

#include <boost/preprocessor/punctuation/comma.hpp>

#define FOO(a)
FOO(std::map<int BOOST_PP_COMMA int>);

:

#define COMMA ,
FOO(std::map<int COMMA int>);
+2

. , , std::map<int, int> -, . decltype(std::map<int, int>()).

+1
source

Just add an extra set of parentheses:

#define FOO(a)
FOO((std::map<int, int>));
+1
source

Source: https://habr.com/ru/post/1606435/


All Articles