I want a function foo
along these lines
template <class T, class Alloc>
void foo(T param, Alloc a) {
vector<int, Alloc<int> > vect_of_ints;
list<float, Alloc<float> > list_of_floats;
do_something()
}
std::allocator a
foo(42, a);
This fails, I think because std::allocator
is not a well defined type till it has been sepcialized for a particular type. Is it possible to do what I want to do, but in some other way.
You can't have one instance of the allocator (a) and expect it to work for 2 different types. You can however, use the allocator generic type (a template template parameter), and specialize it in your foo() in two different ways. You are not using "a" on your foo() anyway.
template <template<class> class Alloc, class T>
void foo(T t1, T t2) {
vector<int, Alloc<int> > vect_of_ints;
list<float, Alloc<float> > list_of_floats;
do_something()
}
// UPDATE: You can use a function wrapper, and then the compiler will be
// able to figure out the other types.
template<class T>
void foo_std_allocator(T t1, T t2)
{
foo<std::allocator, T>(t1, t2);
}
int main()
{
//std::allocator a;
//foo<std::allocator>();
foo<std::allocator, int>(1, 2);
// in the call below, the compiler easily identifies T as int.
// the wrapper takes care of indicating the allocator
foo_std_allocator(1, 2);
return 0;
}