Search code examples
functionc++11gettuplesmember

Why c++11 defines get<>(tuple) as a global function but not a member of tuple?


Seems std::get is just used on tuple class. Why not make it member class of tuple in standard library, any other usages?


Solution

  • The reason get is a non-member function is that if this functionality had been provided as a member function, code where the type depended on a template parameter would have required using the template keyword.

    source.

    Snippet code when get is non-member function:

    template<class T>
    void foo ( tuple<T>& t ) {
        get<0>(t) = 10; // get is non-member function
    }
    

    and another if get is member function of tuple:

    template<class T>
    void foo ( tuple<T>& t ) {
        t. template get<0>() = 10; // ugly
    }
    

    Which version of get usage do you prefer ? For me, the first is better.