Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract template type from initializer list

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 ints. 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.

like image 560
vsoftco Avatar asked Jun 06 '26 16:06

vsoftco


1 Answers

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.

like image 146
rici Avatar answered Jun 08 '26 09:06

rici



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!