Problem with typename.

S

StephQ

First of all: distinction of keywords typename and class in template
arguments.
Accoarding to a post in a well known moderated group:

"There are three possibilities for template arguments:

1) type as in "template <typename T>" or "template <class T>"
2) non-type as in "template <int N>"
3) class template as in "template <template<typename> class C>" "

Now, in 1 class and typename are equivalent.
1 is parametrizing the type of the object
2 is parametrizing which object

In 3 you have to use class, because C is just a template class.
But I admit I am a little confused here, what is exactly C? Could I
pass stl vector as C ? So template<typename> really means only that C
is a class template (and not a type), but actually could be defined by
more than 1 template argument? Where could I find a concrete easy
example?

Also, what is considered a good programming syntax? Using typename in
1 and 2, while class in 3?


Another problem I'm facing is that gcc requires the typename keyword
in a lot of situations, like:

template <class T>
void print(const std::vector<T>& vec)
{
std::vector<T>::const_iterator i;
...
}

warning: `std::vector<T, std::allocator<_CharT> >::const_iterator' is
implicitly a typename (deprecated)

I don't know where should I add the typename keyword, and why.

Thank you

Cheers
StepQ

p.s. Is there a way to write a print function once for list, vector,
deque, .... ?
 
V

Victor Bazarov

StephQ said:
First of all: distinction of keywords typename and class in template
arguments.
Accoarding to a post in a well known moderated group:

"There are three possibilities for template arguments:

1) type as in "template <typename T>" or "template <class T>"
2) non-type as in "template <int N>"
3) class template as in "template <template<typename> class C>" "

Now, in 1 class and typename are equivalent.
1 is parametrizing the type of the object

1 is a beginning of a template declaration. WHAT it parameterises
(a type or a function) is defined _after_ the closing angle bracket.

template<typename T> class A; // declares a class template "A"
template said:
2 is parametrizing which object

Uh... I don't understand that statement.
In 3 you have to use class, because C is just a template class.
Huh?

But I admit I am a little confused here, what is exactly C?

C is a template with exactly 1 argument which is itself a type.
Could I
pass stl vector as C ?

Mmm... No. 'std::vector' has more than 1 argument.
So template<typename> really means only that C
is a class template (and not a type), but actually could be defined by
more than 1 template argument? Where could I find a concrete easy
example?

On the web?
Also, what is considered a good programming syntax?

Any syntax your compiler doesn't barf at is "good programming syntax".
Using typename in
1 and 2, while class in 3?

Doesn't matter. Either will do.
Another problem I'm facing is that gcc requires the typename keyword
in a lot of situations, like:

template <class T>
void print(const std::vector<T>& vec)
{
std::vector<T>::const_iterator i;
...
}

warning: `std::vector<T, std::allocator<_CharT> >::const_iterator' is
implicitly a typename (deprecated)

I don't know where should I add the typename keyword, and why.

To disambiguate anything as a type name. Usually it is required with
"dependent names". Look it up.
p.s. Is there a way to write a print function once for list, vector,
deque, .... ?

Yes, it's called "std::copy" or "std::transform" or "std::for_each".

std::copy(mylist.begin(), mylist.end(),
std::eek:stream_iterator<int>(std::cout, ", "));

V
 
S

Sylvester Hesp

StephQ said:
First of all: distinction of keywords typename and class in template
arguments.
Accoarding to a post in a well known moderated group:

"There are three possibilities for template arguments:

1) type as in "template <typename T>" or "template <class T>"
2) non-type as in "template <int N>"
3) class template as in "template <template<typename> class C>" "

Now, in 1 class and typename are equivalent.
1 is parametrizing the type of the object

T is a template type argument
2 is parametrizing which object

N is a template non-type argument
In 3 you have to use class, because C is just a template class.
But I admit I am a little confused here, what is exactly C?

C is a template template argument. You can pass it a template name that
corresponds to the template argument signature.

Could I pass stl vector as C ?

Well, no, because std::vector has more than one template argument. The
exact signature for std::vector is template<typename, typename> (the
second argument is for passing custom allocators)

So template<typename> really means only that C is a class template (and not a
type), but actually could be defined by more than 1 template argument? Where
could I find a concrete easy example?

I'm not sure what you mean, but it means that C is itself a template,
and you can use it as such. For example:

template<class T> struct A { };

template<template<class> class U> struct B
{
U<int> myTemplateInstance;
};

Since A has a single template type argument, which complies to the
signature of the template template parameter U in B<U,T>, you can use A
as argument to B:

B said:
Another problem I'm facing is that gcc requires the typename keyword
in a lot of situations, like:

template <class T>
void print(const std::vector<T>& vec)
{
std::vector<T>::const_iterator i;
...
}

warning: `std::vector<T, std::allocator<_CharT> >::const_iterator' is
implicitly a typename (deprecated)

I don't know where should I add the typename keyword, and why.

This has to do with so called dependent names. At the point of
definition of print(), the compiler does not know what
std::vector<T>::const_iterator is, because it does not yet know what T
is. Therefore, the definition of std::vector<T>::const_iterator is
dependent on T and is called a dependent name.

The compiler needs to be able to destinguish type names from non type
names at the point of definition, and since it can't, it always assumes
it is a non type. The meaning of dependent names can differ between
template specializations:

template<class T> struct S
{
typedef int foo;
};

template<> struct S<char>
{
void foo();
};

Here, S<int>::foo is a type, whereas S<char>::foo is a function.

So, because the compiler does not yet know what T is, it assumes that
std::vector<T>::const_iterator is a variable or a function, making your
function definition ill-formed. To let the compiler know you're talking
about a type, you have to manually prefix the complete type with the
keyword 'typename', like so:

typename std::vector<T>::const_iterator i;

When nested templates within templates are concerned, you even have to
specify the 'template' keyword, otherwise the compiler interprets it as
if you're using the less-than operator, rather than instantiating a
template:

template<class T> struct A
{
template<class U> struct B { };
};

template<class T> void foo()
{
typename A<T>::template B<int> b;
}

A subtle difference in the use of 'typename' and 'template' is that you
use 'typename' only once, before the complete type name, while you use
'template' everytime you instantiate a nested template, right between
the :: (or . or ->) and the template name.

- Sylvester
 
S

StephQ

1 is a beginning of a template declaration. WHAT it parameterises
(a type or a function) is defined _after_ the closing angle bracket.

template<typename T> class A; // declares a class template "A"
template<typename T> void foo(int); // declares a function template

I agree.
Uh... I don't understand that statement.

I mean that the distinction between 1 and 2 is that in 1 you are
parametrizing the type, while in 2 the particular object (or a given
type, which could be parametrized by a previous template declaration).
If we had function template you could completely avoid using 2 and
pass additional function arguments.
This is not the case of a template class (which doesn't have function
argument :p )

I mean that using typename instead of class in this situation does not
make sense.

C is a template with exactly 1 argument which is itself a type.

A class (in the standard sense) which is templatized: a template
class.
C has to be a class (not a template type) Or am I wrong?
OK I admit I don't know the difference between template class and
class template.
Mmm... No. 'std::vector' has more than 1 argument.


On the web?
:p


Any syntax your compiler doesn't barf at is "good programming syntax".

But following a guide like:
http://geosoft.no/development/cppstyle.html
helped my productivity. I don't like/follow all the suggestions but it
proved very useful anyway, for me at least.

Cheers
StephQ
 
S

StephQ

1 is a beginning of a template declaration. WHAT it parameterises
(a type or a function) is defined _after_ the closing angle bracket.

template<typename T> class A; // declares a class template "A"
template<typename T> void foo(int); // declares a function template

I agree.
Uh... I don't understand that statement.

I mean that the distinction between 1 and 2 is that in 1 you are
parametrizing the type, while in 2 the particular object (or a given
type, which could be parametrized by a previous template declaration).
If we had function template you could completely avoid using 2 and
pass additional function arguments.
This is not the case of a template class (which doesn't have function
argument :p )

I mean that using typename instead of class in this situation does not
make sense.

C is a template with exactly 1 argument which is itself a type.

A class (in the standard sense) which is templatized: a template
class.
C has to be a class (not a template type) Or am I wrong?
OK I admit I don't know the difference between template class and
class template.
Mmm... No. 'std::vector' has more than 1 argument.


On the web?
:p


Any syntax your compiler doesn't barf at is "good programming syntax".

But following a guide like:
http://geosoft.no/development/cppstyle.html
helped my productivity. I don't like/follow all the suggestions but it
proved very useful anyway, for me at least.

Cheers
StephQ
 
V

Victor Bazarov

StephQ said:
StephQ said:
First of all: distinction of keywords typename and class in template
arguments.
Accoarding to a post in a well known moderated group:
"There are three possibilities for template arguments:
1) type as in "template <typename T>" or "template <class T>"
2) non-type as in "template <int N>"
3) class template as in "template <template<typename> class C>"
" [..]
But I admit I am a little confused here, what is exactly C?

C is a template with exactly 1 argument which is itself a type.

A class (in the standard sense) which is templatized: a template
class.

That's incorrect terminology. Not "a template class". A class
template.
C has to be a class (not a template type) Or am I wrong?

Yes, you are. C is (has to be) a class template.
OK I admit I don't know the difference between template class and
class template.

The former doesn't exist.
[..]
Also, what is considered a good programming syntax?

Any syntax your compiler doesn't barf at is "good programming
syntax".

But following a guide like:
http://geosoft.no/development/cppstyle.html
helped my productivity.

Don't confuse style and syntax, please.
I don't like/follow all the suggestions but it
proved very useful anyway, for me at least.

Sure

V
 
P

Pete Becker

Victor said:
The former doesn't exist.

Well, yes, but it did. The difference was that a class template was a
template that could be used to define a class; a template class was a
class that was defined using a template. The new term for the latter is
specialization.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
S

StephQ

Thank you for you reply.
The last part on typenames was very clarifying!

Cheers
StephQ\
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top