Re: template args
kvnil ha scritto:
------------- warning: the code below doesn't compile with VS2005, see
comment
---------------
#include <vector>
#include <list>
using namespace std;
template<class T, template<class,class> class U>
class wrapper {
U<T> holder; // doesn't compile!
U is declared to be a template expecting two arguments and you are
providing only one of them. You must provide both. Please notice that in
this context you can't access any default argument that actual argument
replacing U might have in the instantiation context.
public:
size_t size() { return holder.size(); }
};
int _tmain(int argc, char* argv[]) {
wrapper<int,vector> x;
wrapper<int,list> y;
printf("%d,%d\n",x.size(),y.size());
}
2.
Or even take this simple example:
How can one avoid specifying arguments in declaration of variable v_ ?
template<typename T1 = float, typename T2 = int> class X1 { };
template<template<typename,typename> class U = X1> class X2 { };
int main(int, char**)
{
X2 v_; // doesn't compile
}
The problem is the same as above, let me explain it with different
words: in the context of function main X1<> has two default parameters,
but in the context of the template wrapper, U<> still has no default
parameters. When instantiating the template, it's just the name X1 that
replaces U, U doesn't get the default parameters that X1 has. In fact
you could even provide U with default parameters which would be valid in
the context of wrapper:
template<class T, template<class I,class = std::allocator<I> > class U>
class wrapper {
...
};
However, please notice that standard container have *at least* two
parameters, but they may have more, so using template template
parameters is not going to be portable, as you must specify the exact
number of parameters. The morale is that template template parameters
are less useful than one may think.
HTH,
Ganesh
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]