I want to write a function that returns a preallocated vector. Since it should work for any kind of vector, I am using a template.
My question is whether it is possible to deduce the template parameter of the function call with the template parameter of the left hand side vector ?
template<typename T>
std::vector<T> createVector(size_t initial) {
std::vector<T> vec;
vec.reserve(initial);
return vec;
}
struct Foo {
};
int main() {
//std::vector<Foo> foos = createVector(4); // this should work
//auto foos = createVector<Foo>(4); // this should be okay, too
std::vector<Foo> foos = createVector<Foo>(4); //duplicate Type :(
}