most vexing parse prevents in-class initializing a std::vector<int>

☆樱花仙子☆ 提交于 2019-12-01 03:16:43

Default member initializers work with = as well. So

struct Foo{
    std::vector<int> v = std::vector<int>(3);
};

Will do it. Though obviously, a major caveat is the fact that we are repeating the type name here.

We can alleviate it somewhat with decltype:

struct Foo{
    std::vector<int> v = decltype(v)(3);
};

But that still has us naming things twice.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!