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
template <typename T> ... This means exactly the same thing as the previous instance. The typename and class keywords can be used interchangeably to state that a template parameter is a type variable (as opposed to a non-type template parameter).
Templates in C++? It is a simple yet powerful tool that acts as a blueprint for creating generic functions or classes. While using a template in C++, you pass a data type as a parameter. Thus, instead of maintaining multiple codes, you have to write one code and give the data type you want to use.
For normal code, you would use a class template when you want to create a class that is parameterised by a type, and a function template when you want to create a function that can operate on many different types.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With