C++ tuple implementation with get as a member If you're familiar with std::tuple, you're aware that accessing its elements is slightly unusual. Instead of using a member function, we're provided with the function std::get instead. I was curious as to why this is, so I conducted a brief search. The few websites that I saw all gave the same reason: the code used to called the method is ugly.

We would expect:

my_tuple.get&lt;0&gt;()


but apparently we would have to write:

my_tuple.template get&lt;0&gt;()


(a source, see footnote 5).

This seemed like a bad case of "I heard that she heard that he heard...", because I was unable to find an implementation of a tuple that exposed this ugliness by including get as a member. I heard that Boost's tuple had get as a member, but after inspecting the source I discovered that it also only accepted 10 elements max. It was time for me to implement it myself!

template &lt;typename... Ts&gt;
class Tuple
{

};

template &lt;std::size_t Index, typename... Rs&gt;
struct TypeHolder;

template &lt;std::size_t Index, typename R, typename... Rs&gt;
struct TypeHolder&lt;Index, Tuple&lt;R, Rs...&gt;&gt;
{
    typedef typename TypeHolder&lt;Index - 1, Tuple&lt;Rs...&gt;&gt;::type type;
};

template &lt;typename R, typename... Rs&gt;
struct TypeHolder&lt;0, Tuple&lt;R, Rs...&gt;&gt;
{
    typedef R type;
};

template &lt;typename T, typename... Ts&gt;
class Tuple&lt;T, Ts...&gt; : public Tuple&lt;Ts...&gt;
{
public:
    Tuple(T obj, Ts... objs)
        : Tuple&lt;Ts...&gt;{ std::forward&lt;Ts&gt;(objs)... }
        , obj{obj}
    {

    }

    template &lt;std::size_t Index&gt;
    typename std::enable_if&lt;
        Index == 0,
        typename TypeHolder&lt;Index, Tuple&lt;T, Ts...&gt;&gt;::type&amp;
    &gt;::type get()
    {
        return obj;
    }

    template &lt;std::size_t Index&gt;
    typename std::enable_if&lt;
        Index != 0,
        typename TypeHolder&lt;Index, Tuple&lt;T, Ts...&gt;&gt;::type&amp;
    &gt;::type get()
    {
        return static_cast&lt;Tuple&lt;Ts...&gt;*&gt;(this)-&gt;get&lt;Index - 1&gt;();
    }

protected:
    T obj;
};


This functions as expected, and after some brief testing I found that there were no issues with storing pointers or references. But there was no need to write "template" for every call to get! (My compiler is GCC, by the way.)

Is there a reason why std::tuple was not implemented in this way? I would like to know about any glaring flaws with this code before I move forward with it.
Your source is not very specific as to why/when my_tuple.template get&lt;0&gt;() is required.

If you have code like this:

Tuple&lt;int, double, float&gt; x(1,2,3);
int i = x.get&lt;0&gt;();


That is fine, because all template arguments to Tuple are known at the time of parsing and Tuple&lt;int, double, float&gt; is instantiated immediately, before the call to get is parsed. At that point the compiler already knows that in that specific instantiation of Tuple get is member function template rather than a non-template member and everything is ok.

This however

template&lt;typename T&gt;
auto foo(T t) {
    Tuple&lt;T,int,double&gt; x(t,2,3);
    return x.get&lt;0&gt;();
}


generates a compiler error, because when foo is parsed, the actual type of T is not known and instantiation of Tuple&lt;T,int,double&gt; needs to be delayed until instantiation of foo. This means that when x.get is parsed, the compiler does not yet know what kind of language construct get is in the instatiation of Tuple&lt;T,int,double&gt; and in fact it may very well vary for different T. Therefore it is assumed that get is a non-template member, which doesn't make sense considering the following template argument list.

Your code sadly does not solve this problem and I don't think it is solvable.

Additionally I noticed that your Tuple is an empty class if an empty type list is given. That is a bit inconsistent. I think at least get should be provided as empty function. Calling it should not be allowed, but referring to the member function should be fine.