Port to gcc-4.3 (template template issue)

N

ndbecker2

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 A> class CONT=std::vector>
class Ring {

};


template<typename Cont>
inline std::eek:stream& operator<<(std::eek:stream& 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 A> class CONT> class Ring
test1.cc:11: error: expected a template of type template<class A>
class
CONT , got template<class _Tp, class _Alloc> class std::vector

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

template<typename A> class 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.
 
D

dizzy

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 A> class 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 A> class CONT> class Ring
test1.cc:11: error: expected a template of type template<class A>
class
CONT , got template<class _Tp, class _Alloc> class 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 A> class 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).
 
N

ndbecker2

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Invalid code.


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).




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).

Thank you so much for the excellent explanation!

It seems to me, though, that this makes template template much less
useful.
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top