简体   繁体   中英

Unable to initialize vector from initializer list in delegating constructor

I have a class MyClass as follows:

class MyClass {
public:
    MyClass(vector<vector<float>> arg) {
       // some code here
    }
    MyClass(std::initializer_list<std::initializer_list<float>> arg)
        : MyClass(vector<vector<float>>(arg)) {
    }
    // other class members
};

I'm trying to use a delegating constructor above, as I want an instance of MyClass to be constructed from both std::initializer_list<std::initializer_list<float>> and vector<vector<float>> types, and the constructor code blocks are identical. So I'm trying to delegate the construction of the nested initializer lists by initializing a multidimensional vector and using its constructor.

However, the above code gives me a compiler error:

no instance of constructor "std::__1::vector<_Tp, _Allocator>::vector [with _Tp=std::__1::vector<float, std::__1::allocator<float>>, _Allocator=std::__1::allocator<std::__1::vector<float, std::__1::allocator<float>>>]" matches the argument list -- argument types are: (std::initializer_list<std::initializer_list<float>>)
In file included from /opt/wandbox/gcc-9.3.0/include/c++/9.3.0/vector:67,
                 from prog.cc:11:
/opt/wandbox/gcc-9.3.0/include/c++/9.3.0/bits/stl_vector.h:622:7: note: candidate: 'std::vector<_Tp, _Alloc>::vector(std::initializer_list<_Tp>, const allocator_type&) [with _Tp = std::vector<float>; _Alloc = std::allocator<std::vector<float> >; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::vector<float> >]'
  622 |       vector(initializer_list<value_type> __l,
      |       ^~~~~~
/opt/wandbox/gcc-9.3.0/include/c++/9.3.0/bits/stl_vector.h:622:43: note:   no known conversion for argument 1 from 'initializer_list<std::initializer_list<float>>' to 'initializer_list<std::vector<float>>'
  622 |       vector(initializer_list<value_type> __l,
      |              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~

What am I doing wrong here?

The initializer_list constructor for std::vector looks like:

vector(std::initializer_list<value_type>)

Where value_type is the first template argument to std::vector . In this case, std::vector<std::vector<float>> has value_type = std::vector<float> -- which means that it can only be constructed from a std::initializer_list<std::vector<float>> directly.

In order for this to work, you will need to update in one of the following ways:

  1. update your std::initializer_list to be std::initializer_list<std::vector<float>> ,
  2. just use the std::vector<std::vector<float>> constructor and ignore using the initializer_list entirely 1 , or
  3. Range construct from the initializer_list<initializer_list<float>> in the constructor`:
     MyClass(std::initializer_list<std::initializer_list<float>> arg) : vec{arg.begin(), arg.end()} { // each iterator points to an initializer_list, which implicitly // constructs each vector }
    Edit: Since this question is asking with respect to delegating constructors , you would be stuck explicitly constructing a temporary vector-of-vectors first:
     MyClass(std::initializer_list<std::initializer_list<float>> arg) : MyClass{std::vector<std::vector<float>>{arg.begin(), arg.end()}}
    This is a case where delegating constructors probably aren't the best solution to your problem. See below.

1 Since std::vector already works properly with initializer lists, even when nested in other types, it would probably be easiest to just ignore adding an explicit std::initializer_list constructor entirely -- and just use move semantics properly in the implementation.

If you're using std::move properly, this constructor should be relatively cheap anyway -- which could make the initializer list functionality come for free.

You need vector of floats in std::initializer_list ;

I believe this is what you are looking for

class MyClass{
        public:
            MyClass(vector<vector<float>> arg ): vec( arg) {
                // some code here
            }
            MyClass(std::initializer_list<std::vector<float>> arg) 
            : vec( arg ) {}; 
            
            // other class members

        private:
            std::vector<vector<float>> vec;
};

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