468,161 Members | 2,028 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,161 developers. It's quick & easy.

Port to gcc-4.3 (template template issue)

On upgrading from gcc-4.1.2 to gcc-4.3, this (stripped down) code is
now
rejected:

#include <vector>
#include <iostream>

template<typename T, template <typename Aclass CONT=std::vector>
class Ring {

};
template<typename Cont>
inline std::ostream& operator<<(std::ostream& os, const Ring<Cont>& r)
{
os << '[';
os << ']';
return os;
}
g++ -c test1.cc
test1.cc:11: error: type/value mismatch at argument 2 in template
parameter
list for template<class T, template<class Aclass CONTclass Ring
test1.cc:11: error: expected a template of type template<class A>
class
CONT , got template<class _Tp, class _Allocclass std::vector

What is a reasonable way to fix this? The problem is that the 2nd
parameter
says

template<typename Aclass CONT, which doesn't match std::vector
because it
has an optional 2nd parameter (class _Alloc).

I don't want to only match class CONT with those having a 2nd
parameter
(class _Alloc) - that is too restrictive.
Jun 27 '08 #1
2 2132
nd*******@gmail.com wrote:
On upgrading from gcc-4.1.2 to gcc-4.3, this (stripped down) code is
now
rejected:

#include <vector>
#include <iostream>

template<typename T, template <typename Aclass CONT=std::vector>
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Invalid code.
g++ -c test1.cc
test1.cc:11: error: type/value mismatch at argument 2 in template
parameter
list for template<class T, template<class Aclass CONTclass Ring
test1.cc:11: error: expected a template of type template<class A>
class
CONT , got template<class _Tp, class _Allocclass std::vector
I am surprised to find out that gcc 4.1.2 allowed that (tested it on mine
with -pedantic -ansi and still allows it, bad gcc).
What is a reasonable way to fix this? The problem is that the 2nd
parameter
says

template<typename Aclass CONT, which doesn't match std::vector
because it
has an optional 2nd parameter (class _Alloc).

I don't want to only match class CONT with those having a 2nd
parameter
(class _Alloc) - that is too restrictive.
The standard clearly specifies that your number and type of template
parameters of a template template parameter must match exactly with the
arguments given (including a default argument). The usual "workaround" is
to properly declare the template template parameter as taking the right
number of template parameters and if you want a default make it so for it,
that is in your case make it:

template<typename T, template <typename A, typename = std::allocator<A
class CONT=std::vector>

C++0x does not remove this restriction (and actually extends it for variable
template parameters too) but I supose C++0x template aliasing feature can
help here (ie you make your code take template template parameter of a
single template parameter but you can create aliases that act like single
parameter templates for std::vector<T, allocator<T and pass that).
--
Dizzy

Jun 27 '08 #2
On May 2, 7:55 am, dizzy <di...@roedu.netwrote:
ndbeck...@gmail.com wrote:
On upgrading from gcc-4.1.2 to gcc-4.3, this (stripped down) code is
now
rejected:
#include <vector>
#include <iostream>
template<typename T, template <typename Aclass CONT=std::vector>

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Invalid code.
g++ -c test1.cc
test1.cc:11: error: type/value mismatch at argument 2 in template
parameter
list for template<class T, template<class Aclass CONTclass Ring
test1.cc:11: error: expected a template of type template<class A>
class
CONT , got template<class _Tp, class _Allocclass std::vector

I am surprised to find out that gcc 4.1.2 allowed that (tested it on mine
with -pedantic -ansi and still allows it, bad gcc).
What is a reasonable way to fix this? The problem is that the 2nd
parameter
says
template<typename Aclass CONT, which doesn't match std::vector
because it
has an optional 2nd parameter (class _Alloc).
I don't want to only match class CONT with those having a 2nd
parameter
(class _Alloc) - that is too restrictive.

The standard clearly specifies that your number and type of template
parameters of a template template parameter must match exactly with the
arguments given (including a default argument). The usual "workaround" is
to properly declare the template template parameter as taking the right
number of template parameters and if you want a default make it so for it,
that is in your case make it:

template<typename T, template <typename A, typename = std::allocator<A
class CONT=std::vector>

C++0x does not remove this restriction (and actually extends it for variable
template parameters too) but I supose C++0x template aliasing feature can
help here (ie you make your code take template template parameter of a
single template parameter but you can create aliases that act like single
parameter templates for std::vector<T, allocator<T and pass that).

--
Dizzy
Thank you so much for the excellent explanation!

It seems to me, though, that this makes template template much less
useful.
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Vio | last post: by
5 posts views Thread by Jeffrey Barish | last post: by
1 post views Thread by Steve | last post: by
9 posts views Thread by MNQ | last post: by
66 posts views Thread by Knady | last post: by
4 posts views Thread by anketm | last post: by
9 posts views Thread by eeh | last post: by
12 posts views Thread by david.brown.0 | last post: by
4 posts views Thread by H J van Rooyen | last post: by
2 posts views Thread by merrittr | last post: by
reply views Thread by kamranasdasdas | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.