Extract template type from initializer list
c++, c++11, templates
Solution
You could just define
template<typename T>
void f(const std::initializer_list<T>& v) {
f(std::vector<T>(v));
}
For this to work, the initializer_list has to be of some unambiguous type, so `f({0, 1.41421, 2.71828, 3.14159 })` won't work, but `f({0.0, 1.41421, 2.71828, 3.14159})` will.
Problem
I have a problem with C++11 templated code. I have a template function ``` template <typename T> f(const std::vector<T>& v) { /* do something here*/ }; ``` When I invoke `f(v)`, where `v` is declared as `std::vector<some_type> v;`, the program compiles just fine. However, if I pass an initializer list to `f`, say `f({a,b,c})`, where `a`, `b`, `c` are all of the same type, say `some_type`, I get a compile error: `couldn't infer template argument 'T'`, so I have to manually specify the type when invoking `f`. That is, for example, `f<int>({a,b,c});` compiles just fine when `a`, `b` and `c` are all `int`s. Is there any way of inferring the template type `T` from a standard initializer list given that the function is defined as taking a parameter of `std::vector<T>`? Basically I just want to be able to invoke `f({initializer_list});` without specifying the type of the elements of the `initializer_list` in angle brackets when invoking `f`.