简体   繁体   English

无法在委托构造函数中从初始值设定项列表初始化向量

[英]Unable to initialize vector from initializer list in delegating constructor

I have a class MyClass as follows:我有一个类MyClass如下:

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.我正在尝试使用上面的委托构造函数,因为我希望从std::initializer_list<std::initializer_list<float>>vector<vector<float>>类型以及构造函数代码构造MyClass的实例块是相同的。 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: std::vector的 initializer_list 构造函数如下所示:

vector(std::initializer_list<value_type>)

Where value_type is the first template argument to std::vector .其中value_typestd::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.在这种情况下, std::vector<std::vector<float>>具有value_type = std::vector<float> -- 这意味着它只能从std::initializer_list<std::vector<float>>构造std::initializer_list<std::vector<float>>直接。

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>> ,将您的std::initializer_list更新为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只需使用std::vector<std::vector<float>>构造函数并忽略使用initializer_list完全1 ,或
  3. Range construct from the initializer_list<initializer_list<float>> in the constructor`:构造函数中initializer_list<initializer_list<float>>的范围构造:
     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. 1由于std::vector已经与初始化列表一起正常工作,即使嵌套在其他类型中,最简单的方法可能是完全忽略添加显式std::initializer_list构造函数——并在实现中正确使用移动语义。

If you're using std::move properly, this constructor should be relatively cheap anyway -- which could make the initializer list functionality come for free.如果您正确使用std::move ,无论如何,这个构造函数应该相对便宜——这可以使初始化列表功能免费提供。

You need vector of floats in std::initializer_list ;您需要std::initializer_listfloats向量;

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;
};

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

相关问题 如何在varargs构造函数的初始化列表中初始化向量? - How can I initialize a vector in the initializer list of a varargs constructor? 初始化程序列表向量构造函数 - initializer list vector constructor 无法在 Visual Studio 代码中使用初始值设定项列表初始化向量 - Unable to initialize vector with initializer list in visual studio code 从构造函数参数初始化类初始化列表中的const多维数组 - Initialize const multidimensional array in class initializer list from constructor arguments std::vector 中的初始化列表构造函数 - initializer list constructor in std::vector 如何在构造函数初始值设定项列表中初始化std :: unique_ptr对象的类成员std :: vector? - How do I initialize a class member `std::vector` of `std::unique_ptr` object in the constructor initializer list? 如何初始化 std::vector<std::time_t> 在构造函数初始值设定项列表中有 n 个元素</std::time_t> - How to initialize std::vector<std::time_t> with n elements in constructor initializer list 在初始化列表中使用空构造函数初始化父类? - Initialize parent class with empty constructor in initializer list? 在C ++中初始化构造函数初始化列表中的结构 - initialize a structure in constructor initializer list in C++ 使用初始化列表在构造函数中初始化复杂的 map - Initialize complex map in constructor with initializer list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM