[英]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_type
是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.在这种情况下,
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:为了使其正常工作,您需要通过以下方式之一进行更新:
std::initializer_list
to be std::initializer_list<std::vector<float>>
,std::initializer_list
更新为std::initializer_list<std::vector<float>>
,std::vector<std::vector<float>>
constructor and ignore using the initializer_list
entirely 1 , orstd::vector<std::vector<float>>
构造函数并忽略使用initializer_list
完全1 ,或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. 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_list
的floats
向量;
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.