简体   繁体   中英

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

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 });

}

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 });

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM