C++ using typedefs in non-inline functions
- by ArunSaha
I have a class like this
template< typename T >
class vector {
public:
typedef T & reference;
typedef T const & const_reference;
typedef size_t size_type;
const_reference at( size_t ) const;
reference at( size_t );
and later in the same file
template< typename T >
typename vector<T>::const_reference // Line X
vector<T>::at( size_type i ) const
{
rangecheck();
return elems_[ i ];
}
template< typename T >
reference // Line Y
vector<T>::at( size_type i )
{
rangecheck();
return elems_[ i ];
}
Line X compiles fine but Line Y does not compile. The error message from g++ (version 4.4.1) is:
foo.h:Y: error: expected initializer before 'vector'
From this I gather that, if I want to have non-inline functions then I have to fully qualify the typedef name as in Line X. (Note that, there is no problem for size_type.)
However, at least to me, Line X looks clumsy.
Is there any alternative approach?