简短的问题:
是否允许连接特殊标志,例如+
, -
对于字符串连接宏##
?例如,
#define OP(var) operator##var
will OP(+)
扩展到operator+
?
确切的问题:
#include "z3++.h"
#include <unordered_map>
namespace z3 {
z3::expr operator+(z3::expr const &, z3::expr const &);
}
typedef z3::expr (*MyOperatorTy)(z3::expr const &, z3::expr const &);
#define STR(var) #var
#define z3Op(var) static_cast<MyOperatorTy>(&z3::operator##var)
#define StrOpPair(var) \
{ STR(var), z3Op(var) }
void test() {
std::unordered_map<std::string, MyOperatorTy> strOpMap1{
{"+", static_cast<MyOperatorTy>(&z3::operator+)}}; // fine
std::unordered_map<std::string, MyOperatorTy> strOpMap2{StrOpPair(+)}; // error
}
For strOpMap2
, using clang++ -c -std=c++11
,它报告:
error: pasting formed 'operator+', an invalid preprocessing token
使用时g++ -c -std=c++11
, 它给:
error: pasting "operator" and "+" does not give a valid preprocessing token
通过阅读gcc 的手册 http://gcc.gnu.org/onlinedocs/cpp/Concatenation.html我发现应该可以连接,但是为什么两个编译器都会发出错误?