c++ - How to use converting constructor to initialize vector -


i have 2 classes, a can constructed string , defines conversion string a.

class {   public:      a(std::string s) : s_(s) {}    private:      std::string s_; };   class b {   public:      b() : v_{"foo", "bar"} {}    private:      std::vector<a> v_; }; 

class b has vector member containing a objects. constructor initializes vector (v_) strings. not work. why?

essentially, std::vector<a> expects constructed using initializer list containing a objects. compiler cannot implicitly convert initializer list of std::string objects 1 containing as, , none of other std::vector constructors take 2 std::strings (and if 1 constructor did, wouldn't have meaning you're looking for).

if write

b() : v_{a("foo"), a("bar")} {} 

instead, works. have initializer list of as, gets interpreted correctly.


Comments