Re: template args
On 4???4???, ??????9???33???, kvnil <kv...@mail.ru> wrote:
Hi,
1.
I have a syntax question (I am not trying to write container independent
code). I would like to specify a container and its type, as for example,
"something<int,vector>" where the "int" would be passed to the "vector".
However, I can't get the syntax right, as apparently the default arguments,
i.e., allocator in the code below, is ignored.
It seems that U<T> is wrong, as U has to have two arguments. However, one
of them should be automatically deduced. Nevertheless it doesn't compile.
------------- 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!
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());
}
by removing the declaration of "x" and "y", we still get the error,
which means that your definition of template class "wrapper" was
wrong,
so the using of "vector"/"list" has nothing to do with the error, the
compiler
does not foresee that you're using a template with default argument.
when you
declared "U<T> holder", the compiler notify that the number of
template parameter
does not match the number of template argument.
solution:
template<class T, template<class Ty, class = std::allocator<Ty> >
class U>
class wrapper {
....
};
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
X2<> v_; // you can't even ignore <>, which indicate X2 is a
template.
}
HTH
--
Best Regards
Barry
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]