101*_*010 1 c++ c-preprocessor c++11
我有以下宏:
#define ASSERT_ITERATOR_VALUE_TYPE(Iterator__, Value_type__) \
static_assert(std::is_same<Value_type__, typename Iterator__::value_type>::value, \
"Expected iterator with value type #Value_type__")
Run Code Online (Sandbox Code Playgroud)
在上面的宏中,我试图Value_type__在作为第二个输入参数的字符串文字中插入/附加标记static_assert.
显然,这不是我想要实现的,因为如果我将宏声明为:
ASSERT_ITERATOR_VALUE_TYPE(std::set<int>::iterator, double);
Run Code Online (Sandbox Code Playgroud)
我会收到消息:
error: static assertion failed: Expected iterator with value type #Value_type__
^^^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)
相反,我想接受这个消息:
error: static assertion failed: Expected iterator with value type double
^^^^^^
Run Code Online (Sandbox Code Playgroud)
是否有某种预处理器巫术可以帮助我达到我想要的目标?
#define ASSERT_ITERATOR_VALUE_TYPE(Iterator__, Value_type__) \
static_assert(std::is_same<Value_type__, typename Iterator__::value_type>::value, \
"Expected iterator with value type " #Value_type__)
Run Code Online (Sandbox Code Playgroud)
您将宏参数扩展为字符串文字,然后依赖字符串文字串联.