我刚注意到,在N3291中标记了一个更改(5.2.1 Subscripting [expr.sub]):
之前,可以operator[]使用新的braced-init-list重载:
struct X {
Z operator[](std::initializer_list<int>);
};
X x;
x[{1,2,3}] = 7; // OK: meaning x.operator[]({1,2,3})
Run Code Online (Sandbox Code Playgroud)
现在删除并替换为:
braced-init-list不能与内置的下标运算符一起使用.
出了什么问题?
这就是代码,并带有错误:"非法成员初始化:'a'不是基础或成员",错误信息的含义是什么,为什么?
class A{
public:
int a;
};
class B:public A{
public:
B();
};
B::B():a(10){ // put "a(10)" into the constructor body is right
}
Run Code Online (Sandbox Code Playgroud) 我的图形构造函数:
Graph(std::initializer_list<Edge> list);
Run Code Online (Sandbox Code Playgroud)
我的边缘构造函数:
Edge(int out, int in);
Run Code Online (Sandbox Code Playgroud)
我想通过以下方式创建我的图表:
Graph g = { (1,2), (3,4), (5,1), (5,3)};
Run Code Online (Sandbox Code Playgroud)
但是如果我写的话,我只想要一个成功的编译:
Graph g = { Edge(1,2), Edge(3,4), Edge(5,1), Edge(5,3)};
Run Code Online (Sandbox Code Playgroud)
如何在没有"Edge"字的情况下在initializer_list创建边缘?
struct vec2{float x,y;};
Run Code Online (Sandbox Code Playgroud)
这将有效:
vec2* window(float h, float w)
{
vec2 ret[4] = {{w,h},{1-w,h},{1-w,1-h},{w,1-h}};
return ret;
}
Run Code Online (Sandbox Code Playgroud)
但这不会:
vec2* window(float h, float w)
{
return {{w,h},{1-w,h},{1-w,1-h},{w,1-h}};
}
Run Code Online (Sandbox Code Playgroud)
我猜这个错误来自于后者没有指定数组大小的事实,但我不知道我是否遗漏了一些东西......
我也尝试过:
return vec2[4]({{w,h},{1-w,h},{1-w,1-h},{w,1-h}});
Run Code Online (Sandbox Code Playgroud) 考虑我有这样的代码:
#include <initializer_list>
class my_class
{
public:
my_class() {}
void operator = (const std::initializer_list<int>&) {} // OK
template<typename ValueType> void operator = (const ValueType&) {} // Failed
};
int main(int argc, char* argv[])
{
my_class instance;
instance = {1, 2};
return 0;
}
Run Code Online (Sandbox Code Playgroud)
第一个复制赋值运算符可以编译为OK instance = {1, 2}.但是,模板版本将失败并出现此类错误:
code.cpp:15:14: error: no viable overloaded '='
instance = {1, 2};
~~~~~~~~ ^ ~~~~~~
code.cpp:3:7: note: candidate function (the implicit copy assignment operator) not viable: cannot convert initializer list argument to 'const …Run Code Online (Sandbox Code Playgroud) 我有简单的代码令人困惑的情况:
struct Item {
size_t span{};
};
int main() {
Item item{1}; // error is here
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在编译时我有以下错误:
test.cpp: In function ‘int main()’:
test.cpp:8:13: error: no matching function for call to ‘Item::Item(<brace-enclosed initializer list>)’
Item i{1};
^
test.cpp:8:13: note: candidates are:
test.cpp:3:8: note: constexpr Item::Item()
struct Item {
^
test.cpp:3:8: note: candidate expects 0 arguments, 1 provided
test.cpp:3:8: note: constexpr Item::Item(const Item&)
test.cpp:3:8: note: no known conversion for argument 1 from ‘int’ to ‘const Item&’
test.cpp:3:8: note: constexpr …Run Code Online (Sandbox Code Playgroud) 这在C++ 11中似乎不起作用:
class B : public A
{
public:
B(const A& a)
: A(a) // parent constructor for passing the parameter
, B() // delegating constructor for init of other members
{};
// ...
};
Run Code Online (Sandbox Code Playgroud)
gcc告诉我an initializer for a delegating constructor must appear alone.
我如何用参数调用父类的构造函数,并调用B类的基本构造函数?(我在B中有一堆其他需要相同行为的构造函数).
现在我正在考虑编写一个私有B::init()函数并在所有构造函数体中使用它,但这很有点像C++ 03.
什么是首选解决方案?
请参阅以下代码:
std::vector<int> v1{1, 2, 3};
std::vector<int> v2 = {1, 2, 3};
Run Code Online (Sandbox Code Playgroud)
我的问题是:
这两者有区别吗?我知道第一个必须是列表初始化,但第二个怎么样?
因为第二个有一个赋值符号,所以我认为编译器会首先使用它std::initializer_list来创建一个临时符号vector,然后使用复制构造函数将temp复制vector到v2.这是事实吗?
c++ initialization initializer-list c++11 list-initialization
std :: initializer_list是否具有复制构造函数,如果有,是否曾经使用过?在哪种情况下?因为我注意到以下内容无法在GCC中编译:
std::initializer_list<int>{{1,2,3,4}};
Run Code Online (Sandbox Code Playgroud)
而下面的一个
class Test{
public:
Test(const std::initializer_list<int> &){}
};
Test{{1,2,3,4,5,6}};
Run Code Online (Sandbox Code Playgroud)
所以我怀疑std :: initializer_list <int>是否具有Test类具有的构造函数,第一段代码将进行编译
我正在为正在读的教科书使用GUI库。用于其中“ Lines”对象的构造函数之一具有以下代码。
struct Lines : Shape { // independent lines
Lines() {}
Lines(initializer_list<Point> lst) : Shape{lst} { if (lst.size() % 2) error("odd number of points for Lines"); }
void draw_lines() const;
void add(Point p1, Point p2) { Shape::add(p1); Shape::add(p2); }
};
Run Code Online (Sandbox Code Playgroud)
我尝试了以下初始化,但是显示E0289错误,说明
no instance of constructor "Graph_lib::Lines::Lines" matches the argument list
Lines li{ {Point{100,100},Point{200,200}},{Point{150,150},Point{250,250}}};
Run Code Online (Sandbox Code Playgroud)
我想知道initializer_list在这种情况下应该如何工作,以及在构造对象时如何使用它。
initializer-list ×10
c++ ×9
c++11 ×6
constructor ×3
arrays ×1
c++14 ×1
curly-braces ×1
inheritance ×1
templates ×1