c++ - std::get<I>( std::tuple& ) 作为模板参数

标签 c++ templates c++11

我有一个 std::tuple的对象。我可以使用 std::get<I>(std::tuple&)获取对象的类型并因此自动创建它们。

但是,当我尝试将该类型用作另一个模板的参数时,出现错误。看...

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... >& ) { }
}

说 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 >() ;

最佳答案

你想要std::tuple_element<I, std::tuple<T...>>::type
std::get不返回类型

关于c++ - std::get<I>( std::tuple& ) 作为模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24634409/

相关文章:

c++ - "Want Speed? Pass by value"有多真实

c++ - FFmpeg 使用 avcodec_decode_video2 解码原始缓冲区

c++ - 关于 C++ 中 sizeof(class) 用法抛出的错误

c++ - 如何比较两个不同断点之间的实例状态

templates - 如何在字段插件中对相同的类使用自定义模板,grails

c++ - 通过模板化类中的成员函数返回不可修改的引用

c++ - 模板、静态变量等的冲突声明

c++ - 您如何决定模板方法的值(value)与引用

c++ - 使用模板对象操作

c++ std::normal_distribution 从文件恢复时给出不一致的随机数