 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
lfeiman888@gmail.com Guest
|
Posted: Sat Oct 22, 2005 6:08 pm Post subject: something about template template parameter!!! |
|
|
when i compile source code from <C++ templates> page 50
#include <iostream>
#include <deque>
using namespace deque;
template <typename T,
template
{
private:
CONT<T> elems;
public:
......
};
int main()
{
.....
}
i compile it using c++ compiler from Microsoft Visual Studio 2005,it
returns error c3201,according to MSDN,it means"the template parameter
list for class template 'template' does not match the template
parameter list for template parameter 'template'"
but when i compile it under g++ 3.3.3 ,it sucess,
i want to know why these happen,thanks every one here.:)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ismail Pazarbasi Guest
|
Posted: Sun Oct 23, 2005 9:58 am Post subject: Re: something about template template parameter!!! |
|
|
Obviously, this is not the code you copied from IDE. Here's what I have
found without any compilation.
1. there is no namespace called deque in any header files you included.
it's either "using std::deque" or "using namespace std"
2. what kind of template is it? class? function? you didn't specify
"class"
template <typename T,
template
????????
{
3. template-template parameters, when type of ttp is a class, you
should use "class" keyword, not typename.
So, the code should be:
#include <iostream>
#include <deque>
using std::deque;
template <typename T, template
class MyTTPClass
{
private:
CONT<T> elems;
public:
......
};
Hope that helps.
Oh, btw, read the same page again and again, you'll see following:
As usual, instead of typename you could use the keyword class for
template parameters. However, CONT is used to define a class and must
be declared by using the keyword class. Thus, the following is fine:
template <typename T,
template // OK
class Stack {
...
};
but the following is not:
template <typename T,
template
class Stack { // ERROR
...
};
Ismail
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Valentin Samko Guest
|
Posted: Sun Oct 23, 2005 10:04 am Post subject: Re: something about template template parameter!!! |
|
|
[email]lfeiman888 (AT) gmail (DOT) com[/email] wrote:
| Quote: | when i compile source code from <C++ templates> page 50
using namespace deque;
|
This does not make any sense as there is no namespace deque.
| Quote: |
template <typename T,
template
|
std::deque has more than one template parameter, and CONT must be a template with exactly
one template parameter. This code should not compile according to the current standard.
You can do this either by adding a helper class
template<class T>
struct deque_helper { typedef std::deque<T> type; };
template<typename T, class CONT = typename deque_helper
or by adding an allocator
template<typename T, template
I understand that STL implementations are not allowed to add extra default template
parameters to the standard containers.
--
Valentin Samko - http://val.samko.info
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Carl Barron Guest
|
Posted: Sun Oct 23, 2005 10:08 am Post subject: Re: something about template template parameter!!! |
|
|
In article <1129987699.783933.132100 (AT) z14g2000cwz (DOT) googlegroups.com>,
<"lfeiman888 (AT) gmail (DOT) com"> wrote:
| Quote: | when i compile source code from <C++ templates> page 50
#include <iostream
#include
using namespace deque;
template
template
{
private:
CONT<T> elems;
public:
......
};
int main()
{
.....
}
i compile it using c++ compiler from Microsoft Visual Studio 2005,it
returns error c3201,according to MSDN,it means"the template parameter
list for class template 'template' does not match the template
parameter list for template parameter 'template'"
but when i compile it under g++ 3.3.3 ,it sucess,
i want to know why these happen,thanks every one here.:)
technically g++ 3,3,3, has a bug or an extension. There is no |
template <class T> deque<T> it is template <class T,class =
std::allocator deque.
... template template parameters much match the signature including
optional parameters.
FYI - CW 9.6 also complains the same way namely:
Error : template parameter mismatch
HelloWorld.cp line 3 template <template
std::deque>
[ 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: Sun Oct 23, 2005 10:11 am Post subject: Re: something about template template parameter!!! |
|
|
[email]lfeiman888 (AT) gmail (DOT) com[/email] wrote:
| Quote: | when i compile source code from <C++ templates> page 50
#include <iostream
#include
using namespace deque;
template
template
|
Missing the name of the struct/class here!!!
| Quote: | {
private:
CONT<T> elems;
public:
......
};
int main()
{
.....
}
i compile it using c++ compiler from Microsoft Visual Studio 2005,it
returns error c3201,according to MSDN,it means"the template parameter
list for class template 'template' does not match the template
parameter list for template parameter 'template'"
but when i compile it under g++ 3.3.3 ,it sucess,
i want to know why these happen,thanks every one here.:)
|
The problem is that class template std::deque does not have one template
parameter but two, to be exact:
template <class T, class Allocator = allocator
class deque;
notice that the second parameter has a default value and so it's usually
not specified. Because of this, deque doesn't match
"template<typename ELEM> class CONT"
I don't know why it works on gcc. It shouldn't.
If you are thinking about changing your template parameter into:
"template<typename ELEM, typename ALLOC> class CONT"
you should resist that urge. In fact the standard allows library
implementor to have use more parameters than those explictly required,
provided they have a default value, so the code would be non-portable.
The best solution is to follow the example of the standard container
adaptors (stack, queue, priority_queue), that is to define your class as:
template <typename T, typename Cont = std::deque
class PutMissingNameHere
{
private:
Cont elems;
public:
......
};
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 |
|
 |
David Abrahams Guest
|
Posted: Mon Oct 24, 2005 12:12 am Post subject: Re: something about template template parameter!!! |
|
|
Alberto Ganesh Barbati <AlbertoBarbati (AT) libero (DOT) it> writes:
| Quote: | The problem is that class template std::deque does not have one template
parameter but two, to be exact:
template <class T, class Allocator = allocator
class deque;
|
This lack of ability to ignore defaults when matching template
template parameters may be a reason to avoid them. MPL placeholder
expressions yield approximately the same expressivity and avoid the
whole problem of default arguments:
#include
#include <deque>
using namespace mpl::placeholders;
template <typename T, class Cont = std::deque<_> >
class MyTTPClass
{
private:
typename boost::mpl::apply<CONT,T>::type elems;
public:
...
};
http://boost-consulting.com/mplbook/metafunctions.html#lambda-and-non-metafunction-templates
Cheers,
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
[ 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: Mon Oct 24, 2005 12:21 am Post subject: Re: something about template template parameter!!! |
|
|
Ismail Pazarbasi wrote:
| Quote: |
3. template-template parameters, when type of ttp is a class, you
should use "class" keyword, not typename.
|
But the OP didn't use typename to introduce ttp CONT. It used class, so
what's the point? Or are you referring to:
template <typename T,
template
^^^^^^^^
this typename here?
That's perfecly legal, because it a template-parameter. "There is no
semantic difference between class and typename in a template-parameter."
(14.1/2)
I agree that the "class" in "class CONT" can't be replaced by
"typename", but that's a completely different issue. It's all in 14.1.
| Quote: | So, the code should be:
#include <iostream
#include
using std::deque;
template
class MyTTPClass
{
private:
CONT<T> elems;
public:
......
};
Hope that helps.
|
It doesn't, because still std::deque does not match "template<class>
class CONT", as std::deque has at least two template parameters.
| Quote: | As usual, instead of typename you could use the keyword class for
template parameters. However, CONT is used to define a class and must
be declared by using the keyword class. Thus, the following is fine:
template <typename T,
template // OK
class Stack {
...
};
but the following is not:
template <typename T,
template
class Stack { // ERROR
...
};
|
Well, that's right, but how is that relevant to OP's question?
Ganesh
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ferdi Smit Guest
|
Posted: Mon Oct 24, 2005 1:08 am Post subject: Re: something about template template parameter!!! |
|
|
"Ismail Pazarbasi" <pazarbasi (AT) gmail (DOT) com> wrote
| Quote: | 3. template-template parameters, when type of ttp is a class, you
should use "class" keyword, not typename.
So, the code should be:
template <typename T, template
class MyTTPClass
{
private:
CONT<T> elems;
public:
......
|
The first typename is fine, only the second must be class. So this is ok:
template <typename T, template
As already stated below the problem was std::deque not having a single
template parameter.
--
Regards,
- Ferdi Smit
[ 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: Tue Oct 25, 2005 12:51 pm Post subject: Re: something about template template parameter!!! |
|
|
David Abrahams wrote:
Looks like black magic, but... gee! how powerful it is!
Thanks for pointing that out.
Ganesh
[ 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: Tue Oct 25, 2005 1:21 pm Post subject: Re: something about template template parameter!!! |
|
|
Valentin Samko wrote:
| Quote: |
I understand that STL implementations are not allowed to add extra default template
parameters to the standard containers.
|
I said exactly the opposite in my post, so one of us must be wrong. I do
not remember what I read that made me think that way... I just quickly
checked in the standard and I could not find a statement that might
support my position, therefore I'm ready to apologize for the noise, if
I was mistaken.
Could anyone please provide an authoritative comment on that?
Thanks in advance,
Ganesh
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
David Abrahams Guest
|
Posted: Tue Oct 25, 2005 4:56 pm Post subject: Re: something about template template parameter!!! |
|
|
Alberto Ganesh Barbati <AlbertoBarbati (AT) libero (DOT) it> writes:
Yeah, MPL lambda expressions are fiendishly clever. Having used them
for a few years now I wonder how I ever lived without them.
Just in case it's not obvious, I'm not blowing my own horn here.
Credit for the ideas behind MPL lambda expressions goes to Aleksey
Gurtovoy, the main author, Peter Dimov, who first saw that the
Boost.Bind paradigm could be made to work for metafunctions, and of
course Jaakko Jarvi and Gary Powell, who developed the runtime Lambda
Library.
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Valentin Samko Guest
|
Posted: Tue Oct 25, 2005 6:23 pm Post subject: Re: something about template template parameter!!! |
|
|
Alberto Ganesh Barbati wrote:
| Quote: | I understand that STL implementations are not allowed to add extra default template
parameters to the standard containers.
I said exactly the opposite in my post, so one of us must be wrong. I do
not remember what I read that made me think that way... I just quickly
checked in the standard and I could not find a statement that might
support my position, therefore I'm ready to apologize for the noise, if
I was mistaken.
Could anyone please provide an authoritative comment on that?
|
This was addressed in the TC.
http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-closed.html#94
--
Valentin Samko - http://val.samko.info
[ 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
|
|