c++ - Extract template type from initializer list -
I have a problem with C ++ 11 templated code. I have a template function
Template & lt; Typename T & gt; F (const std :: vector & lt; T & gt; & amp; v) {/ * do something here;} When I call f (v) , where v is std :: vector & lt; some_type & gt; V; , the program is just fine. However, if I give an initial list on f then say f ({a, b, c}), where a , b < / Code>, c are all of the same type, say some_type , I get a compilation error: template logic 't' / code>, therefore For example, f f . Int & gt; ({A, b, c}); aggregates when a , b and c all int s are any standard initializer list There is no way to specify the template type T from the std :: vector & lt; T & gt; is defined as taking the parameters? Actually I just want to be able to call f ({initializer_list}); f without specifying the type of elements of initializer_list .
You can just define
template < typename t & gt; Void f (const std :: initializer_list & lt; t & gt; and v) {f (std :: vector & lt; T & gt; (v)); } To work for this, initializer_list should be of some specific type, hence f ({0, 1.41421, 2.71828, 3.14159}) will not be Work, but f ({0.0, 1.41421, 2.71828, 3.14159}) will be.
Comments
Post a Comment