Search code examples
c++c++11returnautodecltype

Why std::begin uses trailing return type syntax?


The range-access function std::begin is declared as follows (for containers):

template< class C >
auto begin( C& c ) -> decltype(c.begin());

I just wonder why it's not simply

template< class C >
decltype(C::begin) begin( C& c );

Is there any difference between these two?


Solution

  • The equivalent code would be

    template< class C >
    decltype(::std::declval<C &>().begin()) begin( C& c );
    

    which is longer and potentially more error-prone.