简体   繁体   中英

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.

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.

vector<S> vs{"vec_abcd"};

Tries to construct the vector with a const char[] parameter which will not work

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

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

Where each comma separated inner curly brace is for each S you want in the vector.

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'

4 At most one user-defined conversion (constructor or conversion function) is implicitly applied to a single value.

So ....

  1. Convert/cast const char * to std::string vector<S> vs{ std::string("vec_abcd") }
  2. Initialize std::string followed by the vector. Two level of initialization would require two level of indirection with 2 level of nested brace initialization as in vector<S> vs{ {"vec_abcd"} } .

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