【发布时间】:2017-04-02 03:43:07
【问题描述】:
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 &”)不是类、命名空间或枚举
有没有办法构建这样的宏?如果没有,有没有其他方法可以解决这个问题?
【问题讨论】:
标签: c++ pointers gcc methods clang