RMHarris157
RMHarris157

Reputation: 279

std::get<I>( std::tuple& ) as template parameter

I have a std::tuple of objects. I can use std::get<I>(std::tuple&) to get the type of the object and hence automatically create them.

However, when I try to use that type as an argument to another template, I get errors. Behold...

class SpecificMessageHandler; // is valid.

template< class MessageT, class HandlerT >
class MessageCallbackHandler : public MessageCallbackInterface
{
  // ... yadda ...
}


template< class T >
class MessageFactory 
{
  // ... yadda ...

  template< std::size_t I = 0, typename ... T >
  inline typename std::enable_if< I < sizeof ... (T), void >::type
  initMsgMap( const std::tuple< T... >& t )
  {
    const auto& msg( std::get<I>( t ) ); // works fine
    m_messageMap[ msg.getMessageID() ] = msg.clone(); // works fine

    new MessageCallbackHandler
      < std::get<I>( t ), SpecificMessageHandler >(); // error, complains about arg 1

    initMsgMap< I + 1, T... >( t );
  }

  // This is the base case, upon which we do nothing.
  //
  template< std::size_t I = 0, typename ... T >
  inline typename std::enable_if< I == sizeof... (T), void >::type
  initMsgMap( const std::tuple< T... >& ) { }
}

Sayeth gcc ...

MessageFactory.H: In member function 'typename std::enable_if<(I < sizeof (T ...)),
void>::type AWHF::MessageFactory<MessageTuple>::initMsgMap(const std::tuple<_Args2 ...>&)':
awhf/MessageFactory.H:266:54: error: type/value mismatch at argument 1 in template
parameter list for 'template<class MessageT, class HandlerT> class
AWHF::MessageCallbackHandler' < std::get<I>( t ), SpecificMessageHandler >() ;

Upvotes: 0

Views: 205

Answers (1)

Jarod42
Jarod42

Reputation: 217283

you want std::tuple_element<I, std::tuple<T...>>::type
std::get doesn't return a type.

Upvotes: 3

Related Questions