Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I initialize std::vector with list-initialization

Tags:

c++

c++11

Why doesn't this work?

#include <vector>

struct A {
   template <typename T> void f(const std::vector<T> &) {}
};

int main() {

   A a;

   a.f({ 1, 2, 3 });

}
like image 309
template boy Avatar asked Dec 30 '12 02:12

template boy


1 Answers

You can initialize a std::vector<T> with list initialization. However, you cannot deduce the template argument T using a std::vector<T> in the argument list and passing the function something which isn't a std::vector<T>. For example, this works:

#include <vector>

template <typename T>
struct A {
   void f(const std::vector<T> &) {}
};

int main() {

    A<int> a;

   a.f({ 1, 2, 3 });

}
like image 149
Dietmar Kühl Avatar answered Oct 26 '22 22:10

Dietmar Kühl