C ++ Accessing this in the syntax of the new Declarator function

When using the new function declarator syntax, and decltypehow can I access the elements? It seems to be thisunavailable:

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;
};

This results in a compiler error:

scratch.cpp:34:25: error: invalid use of ‘this’ at top level

My compiler g++ 4.6.2.


My workaround is to declare a static member with a name of selfthe same type as the class, which has two problems:

  • He will not automatically select CV qualifiers, for example this.
  • I need to move member declarations above usage decltypeor not see this member (although this is more like a compiler error).
+5
source share
2 answers

GCC 4.7. 4.6 this, .

, .

+3

. - , . , . , ( , , -, , , ).

template <typename Func>
struct context_binder
{
private:
    Func func;

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)...);
    }
};

, 2), , , , , .

+2

All Articles