By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,977 Members | 1,352 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,977 IT Pros & Developers. It's quick & easy.

Problem with typename.

P: n/a
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<typenameclass 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<typenamereally 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, .... ?

May 16 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
StephQ wrote:
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<typenameclass 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 Tclass A; // declares a class template "A"
template<typename Tvoid foo(int); // declares a function template
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<typenamereally 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::ostream_iterator<int>(std::cout, ", "));

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 16 '07 #2

P: n/a
StephQ wrote:
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<typenameclass 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<typenamereally 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 Tstruct A { };

template<template<classclass Ustruct B
{
U<intmyTemplateInstance;
};

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<Ab; // b.myTemplateInstance is of type A<int>
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 Tstruct 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 Tstruct A
{
template<class Ustruct B { };
};

template<class Tvoid foo()
{
typename A<T>::template B<intb;
}

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
May 16 '07 #3

P: n/a
On May 16, 10:02 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
StephQ wrote:
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<typenameclass 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 Tclass A; // declares a class template "A"
template<typename Tvoid foo(int); // declares a function template
I agree.
2 is parametrizing which object

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 )
In 3 you have to use class, because C is just a template class.
I mean that using typename instead of class in this situation does not
make sense.

>
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.
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.
>
Could I
pass stl vector as C ?

Mmm... No. 'std::vector' has more than 1 argument.
So template<typenamereally 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?
:P
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. I don't like/follow all the suggestions but it
proved very useful anyway, for me at least.

Cheers
StephQ

May 17 '07 #4

P: n/a
On May 16, 10:02 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
StephQ wrote:
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<typenameclass 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 Tclass A; // declares a class template "A"
template<typename Tvoid foo(int); // declares a function template
I agree.
2 is parametrizing which object

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 )
In 3 you have to use class, because C is just a template class.
I mean that using typename instead of class in this situation does not
make sense.

>
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.
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.
>
Could I
pass stl vector as C ?

Mmm... No. 'std::vector' has more than 1 argument.
So template<typenamereally 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?
:P
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. I don't like/follow all the suggestions but it
proved very useful anyway, for me at least.

Cheers
StephQ

May 17 '07 #5

P: n/a
StephQ wrote:
On May 16, 10:02 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>StephQ wrote:
>>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<typenameclass 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 18 '07 #6

P: n/a
Victor Bazarov wrote:
>
>OK I admit I don't know the difference between template class and
class template.

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)
May 18 '07 #7

P: n/a
Thank you for you reply.
The last part on typenames was very clarifying!

Cheers
StephQ\
May 22 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.