Search code examples
c++classc++11auto

What is the type of an 'auto' return type when returning *this in an anonymous class?


In this code:

struct
{
    auto operator[](const char*)
    {
        return *this;
    }

} m_some_class;

What is type of auto in here?


Solution

  • What is type of auto in here ?

    The type is decltype(m_some_class) - I.e., the return value is of the same type as the variable m_some_class.


    Note that the function will return a copy of *this.

    If a reference to *this is wanted instead, you can use auto& or, since C++14, the more generic decltype(auto).