C++在New Function Declarator语法中访问它

Tra*_*kel 5 c++ c++11

使用新函数声明符语法时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;
};
Run Code Online (Sandbox Code Playgroud)

这会产生编译器错误:

scratch.cpp:34:25: error: invalid use of ‘this’ at top level
Run Code Online (Sandbox Code Playgroud)

我的编译器是g++ 4.6.2.


我的解决方法是声明一个self与类相同类型的静态成员,它有两个问题:

  1. 它不会像自己this那样自动获取CV限定符.
  2. 我必须将成员声明移到使用之上,decltype否则它无法看到成员(虽然这看起来更像是编译器错误).

Rob*_*edy 3

升级到 GCC 4.7。4.6 版不支持this您尝试使用它的地方。

另一个问题涵盖了您可能可以使用的一些解决方法。