加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 为什么我不能用list-initialization初始化std :: vector

发布时间:2020-12-16 04:58:58 所属栏目:百科 来源:网络整理
导读:为什么这不起作用? #include vectorstruct A { template typename T void f(const std::vectorT ) {}};int main() { A a; a.f({ 1,2,3 });} 解决方法 您可以初始化std :: vector T列表初始化.但是,您不能使用std :: vector T推断出模板参数T.在参数列表中并
为什么这不起作用?
#include <vector>

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

int main() {

   A a;

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

}

解决方法

您可以初始化std :: vector< T>列表初始化.但是,您不能使用std :: vector< T>推断出模板参数T.在参数列表中并传递函数不是std :: vector< T>.例如,这有效:
#include <vector>

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

int main() {

    A<int> a;

   a.f({ 1,3 });

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读