简体   繁体   English

C ++向量列表初始化程序无法与我的课程的类型转换构造函数一起使用

[英]c++ vector list initializer not working with type converting constructor for my class

I created a class with aa "type converting constructor" (constructor that takes a single argument of a different type). 我创建了一个带有“类型转换构造函数”(接受不同类型的单个参数的构造函数)的类。 I'm not able to use list initialization syntax to create a vector of that class. 我无法使用列表初始化语法来创建该类的向量。

Wrapping my class within a Boost Variant somehow makes the same class work with similar syntax. 将我的课程包装在Boost Variant中,可以使相同的课程以相似的语法工作。

What is the minimum I need to do to able to add my class into a vector using list initializer syntax? 为了使用列表初始化语法将类添加到向量中,我最少要做什么?

Full program: 完整程序:

#include <boost/variant.hpp>
#include <iostream>
#include <string>
#include <vector>

using namespace std;
using boost::variant;

struct S {
  string s;
  S() {}
  ~S() {}
  S(const string& _s) : s(_s) {
    // Type converting constructor.
  }
};

int main() {
  // This works.
  S x{"abcd"};
  cout << "x: " << x.s << endl;

  // Why does this not compile?
  // I'm trying to create a vector with a single element in it.
  vector<S> vs{"vec_abcd"};

  // This works.
  vector<boost::variant<string>> vnts{"vnt_abcd0"};
  cout << "vec: " << boost::get<string>(vnts[0]) << endl;
}

You need another set of curly braces to use the std::initializer_list constructor. 您需要另一组花括号才能使用std::initializer_list构造函数。

vector<S> vs{"vec_abcd"};

Tries to construct the vector with a const char[] parameter which will not work 尝试使用const char[]参数构造向量,该参数将不起作用

vector<S> vs{{"vec_abcd"}};

On the other hand initializes the vector with a single element initializer list. 另一方面,使用单个元素初始化程序列表初始化向量。 Look at it like 看起来像

vector<S> vs{{"vec_abcd"}};
            |^list data ^|
            ^ ctor call  ^

Live Example 现场例子

Additionally if you wanted to constrict multiple S 's in the vector you can use 另外,如果您想限制向量中的多个S ,则可以使用

vector<S> vs{{"a"}, {"b"}, {"c"}, ..., {"z"}};

Where each comma separated inner curly brace is for each S you want in the vector. 每个逗号分隔的内部花括号都对应于向量中所需的每个S

You are trying to initialize vector, which has a constructor S(const string& _s) with the type const char * and as per the C++ Standard (SC22-N-4411.pdf) section 12.3.4 titled 'Conversions' 您正在尝试初始化向量,该向量具有类型为const char *且根据C ++标准(SC22-N-4411.pdf)标题为“转换”的12.3.4节的构造函数S(const string& _s)

4 At most one user-defined conversion (constructor or conversion function) is implicitly applied to a single value. 4最多将一个用户定义的转换(构造函数或转换函数)隐式应用于单个值。

So .... 所以...

  1. Convert/cast const char * to std::string vector<S> vs{ std::string("vec_abcd") } 将const char *转换/ vector<S> vs{ std::string("vec_abcd") }为std :: string vector<S> vs{ std::string("vec_abcd") }
  2. Initialize std::string followed by the vector. 初始化std::string然后初始化向量。 Two level of initialization would require two level of indirection with 2 level of nested brace initialization as in vector<S> vs{ {"vec_abcd"} } . vector<S> vs{ {"vec_abcd"} }两个级别的初始化将需要两个级别的间接vector<S> vs{ {"vec_abcd"} }以及两个级别的嵌套括号初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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