A hypothetical variadic template tuple class would, as far as I can tell, have to use getters with template parameters.
int MyInt = MyTuple.Get<int>(0);
This is inconvenient, and introduces potential for error. I can’t help but feel that there’s a way to construct the class so you you don’t have to explicitly specify it.
int MyInt = MyTuple.Get(0);
My first thought was for the Get() member function to return another class that figures out the type on its own, probably by comparing typeid(Foo).name() to values in some precomputed list. That still has to happen before runtime, though, and I couldn’t figure out a way to iterate through anything like that at compile-time.
Is there any way for a variadic template container class – like a tuple – to have a getter that doesn’t require the type to be explicitly specified?
You mean like
std::tuple?The getter’s template argument specifies the index of the member, not the type. By definition, the number and types of a tuple are fixed at compile time. Since the type depends on the index, and the type must be known at compile time, the getter must be templated.
etc.