代码:
template< typename... TemplateArguments >
class Tuple
{}
template< typename ExposedType, typename... TemplateArguments >
class Tuple< ExposedType, TemplateArguments... > : public Tuple< TemplateArguments... >
{
public:
template < uint64_t ArgumentNumber >
typename std::enable_if< ArgumentNumber != 1, typename TypeHolder< ArgumentNumber, Tuple< ExposedType, TemplateArguments... > >::m_type& >::m_type Entry();
template < uint64_t ArgumentNumber >
typename std::enable_if< ArgumentNumber == 1, typename TypeHolder< 1, Tuple< ExposedType > >::m_type& >::m_type Entry();
ExposedType m_data;
};
template< uint64_t ArgumentNumber, typename ExposedType >
class TypeHolder
{};
template< typename ExposedType, typename... TemplateArguments >
class TypeHolder< 1, Tuple< ExposedType, TemplateArguments... > >
{
public:
typedef ExposedType m_type;
};
template< uint64_t NumberOfArguments, typename ExposedType, typename... TemplateArguments >
class TypeHolder< NumberOfArguments, Tuple< ExposedType, TemplateArguments... > >
{
public:
typedef typename TypeHolder< NumberOfArguments - 1, Tuple< TemplateArguments... > >::m_type m_type;
};
并执行输入方法:
template< typename ExposedType, typename... TemplateArguments >
template < uint64_t ArgumentNumber >
typename std::enable_if< ArgumentNumber != 1, typename TypeHolder< ArgumentNumber, Tuple< ExposedType, TemplateArguments... > >::m_type& >::m_type Tuple< ExposedType, TemplateArguments... >::Entry()
{
Tuple< TemplateArguments... >& baseTuple = *this;
return ( baseTuple.EntryL< ArgumentNumber - 1 >() );
}
template< typename ExposedType, typename... TemplateArguments >
template < uint64_t ArgumentNumber >
typename std::enable_if< ArgumentNumber == 1, typename TypeHolder< 1, Tuple< ExposedType > >::m_type& >::m_type Tuple< ExposedType, TemplateArguments... >::Entry()
{
return ( m_data );
}
关键是要创建一个元组对象,该对象具有一种方法,当使用时应返回对所需元素的引用:
Tuple< int, int > tuple_1( 8, 10 );
tuple_1.Entry< 1 >() = 14;
尝试编译时出现错误:
error: expected primary-expression before ‘)’ token
return ( baseTuple.Entry< ArgumentNumber - 1 >() );
^
有人可以帮助解决这个问题吗?或显示正确的方法怎么做?
谢谢!
编辑:
如果我添加true,它将起作用:
return ( baseTuple.EntryL< ArgumentNumber - 1 >( true ) );
为什么?
答案 0 :(得分:1)
从属名称,您应该添加template
:
return baseTuple.template Entry<ArgumentNumber - 1>();