clang和gcc之间有一些差异。其中一个差异是它们如何处理指向方法的指针。给定以下代码:
template <typename T_Class, typename T_Ret, typename ... Args>
void store_method(T_Class *object, T_Ret (T_Class::*method)(Args ... args));
class SomeObjectWithPotentiallyLongName {
int commonly_used_method(int var);
void register_method() {
/* The code below is okay for gcc with -std=gnu++11. But clang
* says:
* 'reference to non-static member function must be called' */
store_method(this, commonly_used_method);
/* To make this fine for clang (and also - gcc), I have to
* extend previous line as next */
store_method(this, &SomeObjectWithPotentiallyLongName::commonly_used_method);
}
}
上面的代码展示了需要在很多地方扩展代码以使其能够被clang编译,而使用gcc可以使代码更加整洁和清晰。
最明显的方法是编写一些宏,将this
和method_name
转换为类似于&TypeOfThis::method_name
的内容。
我的想法是使用decltype
:
#define STORE_METHOD(method) store_method(this, (decltype(*this))::method)
void SomeObjectWithPotentiallyLongName::register_method() {
STORE_METHOD(commonly_used_method);
}
但是使用 clang
编译时,此代码会产生以下错误:
'decltype(*this)'(即 'SomeObjectWithPotentiallyLongName &')不是类、命名空间或枚举
有没有办法构建这样的宏?如果没有,是否有其他解决方案?
T&
是一种类型。它可能不是你想要的类型,但它确实是一种类型。 - Nir Friedman