I'm new to C++ and trying to return a value from the size()
member of my X
class; the return type must be of X::size_type
.
template <class T> class X {
public:
typedef size_t size_type;
size_type size() const;
// ...
}
template <class T> X<T>::size_type X<T>::size() const {
// ...
}
The above code results in errors. I added typename
before X<T>::size_type
and the issue seems to be fixed but I don't understand why...
Is this the right way?
Due to the complex and sometimes ambiguous syntax of C++, sometimes hints are needed for the parser/compiler.
In general when referring to types defined inside a class, typename
is needed to hint the compiler that you're talking about a type and not a value (since X<T>::size_type
can be either).