You forgot typedef
. The typename
just says that you are going to use a typename that at the point of the template declaration is not yet known as a type. If you actually want to create a typedef, you actually need that keyword in addition. And i think you forgot to actually name the type when you use it below:
template < class T >
struct S {
typedef typename add_reference< T >::type reference;
};
...
typedef Bar< Foo > type;
S< type >::reference s = some_foo; // initialize!
Remember to initialize the reference. If you know in advance that T
is never a reference (to avoid the reference-to-reference problem) you can also do this directly:
template < class T >
struct S {
typedef T &reference;
};
typedef Bar< Foo > type;
S< type >::reference s = some_bar_foo; // initialize!
If what you wanted to do is to create a reference data member, your syntax without typedef
was correct
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"
I do not know what you want to do exactly.