你忘了typedef
。只是说你将typename
使用一个在模板声明时还不被称为类型的类型名。如果你真的想创建一个 typedef,你实际上还需要那个关键字。而且我认为您在下面使用它时忘记了实际命名类型:
template < class T >
struct S {
typedef typename add_reference< T >::type reference;
};
...
typedef Bar< Foo > type;
S< type >::reference s = some_foo; // initialize!
请记住初始化引用。如果您事先知道T
它永远不是引用(以避免引用到引用的问题),您也可以直接执行此操作:
template < class T >
struct S {
typedef T &reference;
};
typedef Bar< Foo > type;
S< type >::reference s = some_bar_foo; // initialize!
如果您想做的是创建引用数据成员,那么您的语法没有typedef
正确
template < class T >
struct S {
typename add_reference< T >::type reference;
};
...
typedef Bar< Foo > type;
S< type > s = { some_bar_foo }; // initialize!
s.reference = some_other_bar_foo; // assign "some_other_bar_foo" to "some_bar_foo"
我不知道你到底想做什么。