I now want to make template that will push some elements to vectors and other types, that support push_back
operators.
I can do it like this
template<typename T>
T fill(size_t n) {
T v;
//(1)
for(size_t i = 0; i < n; ++i){
v.push_back(generate_some_how());
}
return v;
}
It works. But now I want to improve speed for types that support it using v.reserve(n);
instead of (1)
. But I want to still be able to compile this code for types that will not compile reserve
Is it some easy way to achieve this?
I know that I may specialize hard-coded types but it doesn't seem to be good.
C++11 is OK.