Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

g++ "is not a type" error

Tags:

c++

gcc

templates

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
like image 909
Walter Nissen Avatar asked Aug 19 '09 17:08

Walter Nissen


People also ask

What does template typename t mean?

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).

How template works in c++?

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.

How is a template class different from a template function?

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.


1 Answers

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.

like image 131
Tobias Avatar answered Nov 11 '22 16:11

Tobias