question on template template parameters

R

REH

GCC accepts this in a template parameter list:

template<class> class T = std::vector

However Visual C++ conplains because the actual template parameter list of
std::vector does not match, because of course the defaulted allocator
parameter. So, technically its correct. But what does the standard say?
Does it allow the above? Stroustrup's "The C++ Programming Language" (SE)
uses the above in several examples.

Thanks,

REH
 
V

Victor Bazarov

REH said:
GCC accepts this in a template parameter list:

template<class> class T = std::vector

However Visual C++ conplains because the actual template parameter list of
std::vector does not match, because of course the defaulted allocator
parameter. So, technically its correct. But what does the standard say?
Does it allow the above? Stroustrup's "The C++ Programming Language" (SE)
uses the above in several examples.

The acceptance of this depends on the implementation of 'std::vector'.
It is possible that the implementation has only one argument for that
template. In that case the code is valid. It is also possible that
the implementation has several arguments, in which case the program that
_assumes_ only one argument is ill-formed.

Until "template typedefs" are introduced into the language, you could
work around the error by wrapping the 'std::vector' into another template:

template<class T> struct vector_def_alloc {
typedef std::vector<T> type; // the allocator used is the default
};

.... template<class> class T = vector_def_alloc::type ...

(or something like that)

V
 
R

REH

Victor Bazarov said:
The acceptance of this depends on the implementation of 'std::vector'.
It is possible that the implementation has only one argument for that
template. In that case the code is valid. It is also possible that
the implementation has several arguments, in which case the program that
_assumes_ only one argument is ill-formed.

Until "template typedefs" are introduced into the language, you could
work around the error by wrapping the 'std::vector' into another template:

template<class T> struct vector_def_alloc {
typedef std::vector<T> type; // the allocator used is the default
};

... template<class> class T = vector_def_alloc::type ...

(or something like that)

V

Thanks Victor. Is there a better way of doing this type of thing than
template template parameters? Basically, I want to be able to instantiate
the class template with different types of containers, but I want a policy
that enforces that the container's element type is a specific type. In
other words, the container type is definable, but the element type is not.

REH
 
V

Victor Bazarov

REH said:
Thanks Victor. Is there a better way of doing this type of thing than
template template parameters? Basically, I want to be able to instantiate
the class template with different types of containers, but I want a policy
that enforces that the container's element type is a specific type. In
other words, the container type is definable, but the element type is not.

Did you mention "Policy"?... Somebody mentioned "policy", anyway... I
definitely heard something that sounded like "policy"...

If you're up to it, take a look at "Modern C++ Design". It's all about
policies and how you use them.

V
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top