当使用新的函数声明语法和
decltype
时,如何访问成员?似乎无法访问this
:template <typename Func>
struct context_binder
{
public:
context_binder(const Func& func) :
func(func)
{ }
template <typename... TArgs>
auto operator ()(TArgs&&... args) const
-> decltype(this->func(std::forward<TArgs>(args)...))
{
return func(std::forward<TArgs>(args)...);
}
private:
Func func;
};
这会导致编译器错误:
scratch.cpp:34:25: error: invalid use of ‘this’ at top level
我的编译器是 g++ 4.6.2
。
我的解决方法是声明一个名为 self
的静态成员,类型与类相同,但存在两个问题:
- 它不能像
this
那样自动获取CV限定符。 - 我必须将成员声明移动到
decltype
的使用之前,否则它无法看到该成员(尽管这似乎更像是编译器的错误)。