 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
JCR Guest
|
Posted: Fri May 05, 2006 9:21 am Post subject: Template - class/typename difference |
|
|
Hello.
The following code does compile:
template<template<typename T> class C, typename T>
C<T> foo(C<T> a, C<T> b)
{
return C<T>();
}
but I am curious about two issues:
1. If I write "typename C" instead of "class C", the code does not
compile any more. How comes? I thought that typename and class were
equivalent in a template declaration
2. It appears that "template<template<typename T> class C>" does not
compile and that the ", typename T> is necessary. Why should I specify
T again, especially outside of the template template parameter?
Many thanks
JCR
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Alberto Ganesh Barbati Guest
|
Posted: Sat May 06, 2006 3:21 pm Post subject: Re: Template - class/typename difference |
|
|
JCR ha scritto:
| Quote: | Hello.
The following code does compile:
template<template<typename T> class C, typename T
C<T> foo(C<T> a, C<T> b)
{
return C<T>();
}
but I am curious about two issues:
1. If I write "typename C" instead of "class C", the code does not
compile any more. How comes? I thought that typename and class were
equivalent in a template declaration
|
There are three types of template arguments:
1) type (the usual case) as in "template <typename T>" or "template
<class T>"
2) non-type as in "template <int N>"
3) template as in "template <template <WHATEVER> class C>"
typename and class are equivalent keyword only when describing arguments
of type 1, as shown. Notice that WHATEVER in case 3 is recursively a
list of template arguments so the argument applies also to WHATEVER, but
in the "class C" that follows the typename keyword is NOT allowed.
| Quote: | 2. It appears that "template<template<typename T> class C>" does not
compile and that the ", typename T> is necessary. Why should I specify
T again, especially outside of the template template parameter?
|
The first typename T only describe a formal parameter of the first
template argument C. The name of formal parameter is completely ignored
and it's not introduced in the rest of the construct. The following
syntaxes are BOTH equivalent to yours:
template<template<typename FORMAL_PARAMETER> class C, typename T>
C<T> foo(C<T> a, C<T> b)
{
return C<T>();
}
template<template<typename> class C, typename T>
C<T> foo(C<T> a, C<T> b)
{
return C<T>();
}
If you think it this way, it's clear why you still need to introduce T
in some way in order to use it in the rest of the definition.
HTH,
Ganesh
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Gene Bushuyev Guest
|
Posted: Sat May 06, 2006 4:21 pm Post subject: Re: Template - class/typename difference |
|
|
"JCR" <jcxxr (AT) yahoo (DOT) com> wrote in message
news:1146795273.440228.5920 (AT) v46g2000cwv (DOT) googlegroups.com...
| Quote: | Hello.
The following code does compile:
template<template<typename T> class C, typename T
C<T> foo(C<T> a, C<T> b)
{
return C<T>();
}
but I am curious about two issues:
1. If I write "typename C" instead of "class C", the code does not
compile any more. How comes? I thought that typename and class were
equivalent in a template declaration
|
Is your question, why the standard specifies it that way, or why you thought
what you thought? The first question is simple, the standard provides lattitude
for class/typename use for template type parameter, but not for
template-template parameter (see 14.1). To answer the second question, you need
to consult your shrink :-)
| Quote: |
2. It appears that "template<template<typename T> class C>" does not
compile and that the ", typename T> is necessary. Why should I specify
T again, especially outside of the template template parameter?
|
Because, otherwise, you didn't specify what <T> is. The
"template<template<typename T> class C, typename T>" is the same as
"template<template<typename> class C, typename T>", the first T is optional. Now
if you rewrite it as you suggested "template<template<typename> class C>", then
what compiler can possibly do with the places where you use T, like C<T>? If
your code doesn't need to know T, then why do use template-template parameter?
You can use a simple template:
template<class C>
C foo(C a, C b)
{
return C();
}
--
Gene Bushuyev (www.gbresearch.com)
----------------------------------------------------------------
There is no greatness where there is no simplicity, goodness and truth. ~ Leo
Tolstoy
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Sat May 06, 2006 4:21 pm Post subject: Re: Template - class/typename difference |
|
|
"JCR" <jcxxr (AT) yahoo (DOT) com> writes:
| Quote: | The following code does compile:
template<template<typename T> class C, typename T
C<T> foo(C<T> a, C<T> b)
{
return C<T>();
}
but I am curious about two issues:
1. If I write "typename C" instead of "class C", the code does not
compile any more. How comes? I thought that typename and class were
equivalent in a template declaration
|
I think you don't mean the entire template declaration, but the
template parameter list.
Even there, typename can be written to declare a formal parameter that
names a type. C doesn't name a type, but a template.
| Quote: | 2. It appears that "template<template<typename T> class C>" does not
compile and that the ", typename T> is necessary. Why should I specify
T again, especially outside of the template template parameter?
|
You don't specify T *again*. The two occurences of T in the template's
parameter list are completely unrelated. It could also read
template <template<typename U> class C, typename T>
or even
template <template<typename> class C, typename T>
The first "typename T" in your form of the declaration only exists to
specify that C has one parameter which is of type typename.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Greg Herlihy Guest
|
Posted: Sat May 06, 2006 4:21 pm Post subject: Re: Template - class/typename difference |
|
|
JCR wrote:
| Quote: | Hello.
The following code does compile:
template<template<typename T> class C, typename T
C<T> foo(C<T> a, C<T> b)
{
return C<T>();
}
but I am curious about two issues:
1. If I write "typename C" instead of "class C", the code does not
compile any more. How comes? I thought that typename and class were
equivalent in a template declaration
|
As a template type, C must be a class type. It cannot be just just any
kind of type.
| Quote: | 2. It appears that "template<template<typename T> class C>" does not
compile and that the ", typename T> is necessary. Why should I specify
T again, especially outside of the template template parameter?
|
Because the first T didn't specify anything. It's just a placeholder
within C, and one that the compiler ignores. If that T were removed,
then it would become clear that T is undefined. To define T, it must be
added to the list of template type parameters:
template< template<typename> class C, typename T>
C<T> foo(C<T> a, C<T> b)
{
return C<T>();
}
Greg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
DaVinci Guest
|
Posted: Sat May 06, 2006 5:21 pm Post subject: Re: Template - class/typename difference |
|
|
JCR wrote:
| Quote: | Hello.
The following code does compile:
template<template<typename T> class C, typename T
C<T> foo(C<T> a, C<T> b)
{
return C<T>();
}
but I am curious about two issues:
1. If I write "typename C" instead of "class C", the code does not
compile any more. How comes? I thought that typename and class were
equivalent in a template declaration
|
beacause the C is class.
| Quote: |
2. It appears that "template<template<typename T> class C>" does not
compile and that the ", typename T> is necessary. Why should I specify
T again, especially outside of the template template parameter?
Many thanks
JCR
BTW,how to invoke the function foo() correctly?, |
vector<int> vi;
vector<int> vi1;
foo<vector<int>,int>(vi,vi1) is not right.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
berlin860@gmail.com Guest
|
Posted: Mon May 08, 2006 1:21 pm Post subject: Re: Template - class/typename difference |
|
|
JCR wrote:
| Quote: | Hello.
The following code does compile:
template<template<typename T> class C, typename T
C<T> foo(C<T> a, C<T> b)
{
return C<T>();
}
but I am curious about two issues:
1. If I write "typename C" instead of "class C", the code does not
compile any more. How comes? I thought that typename and class were
equivalent in a template declaration
2. It appears that "template<template<typename T> class C>" does not
compile and that the ", typename T> is necessary. Why should I specify
T again, especially outside of the template template parameter?
|
let me try to answer your question:
1. you said : I thought that typename and class were equivalent in a
template declaration
but it is wrong. typename and class are equivalent only when they are
in a template
parameters. that means
template<class T>
is equal to
template<typename T>, but
template<typename T> class C
is not equal to
template<typename T> typename C
and the latter is an error! in c++ the only two way to declear and
define user-define
type is using class and struct keyword, not typename.
you can find more details about typename and class in
TC++PL 3/e and Effective C++ 3/e Item 42.
2. you should know in
template<template<typename T> class C, typname T>
the two template parameters, the first typename T is different from the
second.
you can write like this : template<template<typename F> calss C,
typename T>
it is also right. what you should exactly kown is template<typename T>
class C
is declear a generic container or something else, but it is generic
type !!
the generic type can have a template parameter T.
when you use the foo like :
vector<int> vi;
foo<vector, int> (vi, vi);
then you know the first template parameter means a generic container.
vector's
template parameter is the second template parameter in
template<template<typename T> class C, typname T>
this is c++ generic programming.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|