Writing a templated function, I declared:
template <typename T>
T invertible(T const& container, T::size_type startIndex, T::size_type endIndex);
Compiling with g++ 4.0.1 I got the error:
error: 'T::size_type' is not a type
Writing a templated function, I declared:
template <typename T>
T invertible(T const& container, T::size_type startIndex, T::size_type endIndex);
Compiling with g++ 4.0.1 I got the error:
error: 'T::size_type' is not a type
You need to add typename.
I.e.
template <typename T>
T invertible(T const& container, typename T::size_type startIndex, typename T::size_type endIndex);
Without having any information on your type T, the compiler needs to know that T::size_type designates a type.
From the standard, section 14.6.2:
A name used in a template declaration or definition and that is dependent on a template-parameter is assumed not to name a type unless the applicable name lookup finds a type name or the name is qualified by the keyword
typename
.
It turned out I needed to specify that T::size_type was a typename. Why is that?
template <typename T>
T invertible(T const& container, typename T::size_type startIndex, typename T::size_type endIndex);
Because during parsing the template declaration, T is not known. So the compiler doesn't know if T::size_type exists at all. It may refer to a static variable, for example. When you later use the template, T is of course known, but the error happens earlier. And please use something less ancient than gcc 4.0.1 ;-)
Edit: If you compile it with -fpermissive, the compiler probably chews your code, but he will give a warning.
As you found out T::size_type needs to be prefixed with typename. Why?
From "C++ Templates: The Complete Guide"
The language definition resolves this problem by specifying that in general a dependent qualified name does not denote a type unless that name is prefixed with the keyword typename.
... The typename prefix to a name is required when the name
- Appears in a template
- Is qualified
- Is not used as a list of base class specifications or in a list of member initializations introducing a constructor definition
- Is dependent on a template parameter
Furthermore the typename prefix is not allowed unless at least the first three previous conditions hold.