Variadic tuple<..> — the get<N>() method
In my last post I talked about how a naive implementation of tuple might be easier to understand, and I showed how you might define the member values with the … operator, if only … were very smart about unpacking (which it isn’t).
The get<N>() method presents additional challenges.
/* fantasy code -- will not work */
template< typename ... TYPEs >
class
tuple
{
// Member variables
private:
TYPEs ...
values;
// Generalized get<N>( ) template method
private:
template< size_t >
void
get( ) const
{ BOOST_STATIC_ASSERT( false); }
// Specialized get<N>( ) methods:
// get<0>( ), get<1>( ), get<2>( ), etc
// Introduces indexof...(), which is like sizeof...()
public:
template< >
decltype( values ) &
get< indexof...( values ) >( )
{ return values ; }
...
// Specialized get<N>( ) const methods
public:
template< >
decltype( values ) const &
get< indexof...( values ) >( ) const
{ return values ; }
...
/* .. etc .. */
};
You can see how this would work if only … could unpack to create a bunch of method specializations. This also requires indexof…(values) which expands into 0, 1, 2, 3 and so on as it unpacks the member variables packed into values.
This would also have to work when a type was const or a reference. But that’s a topic for another day.
Comments
Leave a Reply