我有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... >& ) { }
}
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 >() ;
答案 0 :(得分:3)
你想要std::tuple_element<I, std::tuple<T...>>::type
std::get
未返回类型。