直接初始化是否等同于直接列表初始化?
Is direct-initialization equivalent to direct-list-initialization?
我有以下例子:
struct S{
int x, y;
}
S s1{1}; // direct-initialization or direct-list-initialization ?
S s2{1, 2}; // direct-initialization or direct-list-initialization ?
S s3(1); // direct-initialization or direct-list-initialization ?
S s4(1, 2); // direct-initialization or direct-list-initialization ?
int i1{10}; // direct-initialization or direct-list-initialization ?
int i2(10); // direct-initialization or direct-list-initialization ?
我的问题
- 我只需要知道上面语句中初始化的类型是什么?
- 这里有标准报价吗?
我已经检查了direct-initialization vs direct-list-initialization (C++)的问题,但还没有严格的答案。
来自 direct initialization's documentation:
T object ( arg );
T object ( arg1, arg2, ... );
(1)
T object { arg };
(2) (since C++11)
T ( other )
T ( arg1, arg2, ... )
(3)
Direct initialization is performed in the following situations:
-
- initialization with a nonempty parenthesized list of expressions or braced-init-lists (since C++11)
-
- initialization of an object of non-class type with a single brace-enclosed initializer (note: for class types and other uses of braced-init-list, see list-initialization)
-
- initialization of a prvalue temporary (until C++17)the result object of a prvalue (since C++17) by functional cast or with a parenthesized expression list
Direct-list-initialization
T object { arg1, arg2, ... };
(1)
T { arg1, arg2, ... }
(2)
现在我们可以使用上面的内容来回答您的问题了。
S s1{1}; // direct-list-initialization from direct-list init point 1 above
S s2{1, 2}; // direct-list-initialization from direct-list init point 1 above
S s3(1); // direct-initialization from direct init point 1 above
S s4(1, 2); // direct-initialization from direct init point 1 above
int i1{10}; // direct-initialization from direct init point 2 above
int i2(10); // direct-initialization from direct init point 1 above
我有以下例子:
struct S{
int x, y;
}
S s1{1}; // direct-initialization or direct-list-initialization ?
S s2{1, 2}; // direct-initialization or direct-list-initialization ?
S s3(1); // direct-initialization or direct-list-initialization ?
S s4(1, 2); // direct-initialization or direct-list-initialization ?
int i1{10}; // direct-initialization or direct-list-initialization ?
int i2(10); // direct-initialization or direct-list-initialization ?
我的问题
- 我只需要知道上面语句中初始化的类型是什么?
- 这里有标准报价吗?
我已经检查了direct-initialization vs direct-list-initialization (C++)的问题,但还没有严格的答案。
来自 direct initialization's documentation:
T object ( arg );
T object ( arg1, arg2, ... );
(1)
T object { arg };
(2) (since C++11)
T ( other )
T ( arg1, arg2, ... )
(3)
Direct initialization is performed in the following situations:
- initialization with a nonempty parenthesized list of expressions or braced-init-lists (since C++11)
- initialization of an object of non-class type with a single brace-enclosed initializer (note: for class types and other uses of braced-init-list, see list-initialization)
- initialization of a prvalue temporary (until C++17)the result object of a prvalue (since C++17) by functional cast or with a parenthesized expression list
Direct-list-initialization
T object { arg1, arg2, ... };
(1)
T { arg1, arg2, ... }
(2)
现在我们可以使用上面的内容来回答您的问题了。
S s1{1}; // direct-list-initialization from direct-list init point 1 above
S s2{1, 2}; // direct-list-initialization from direct-list init point 1 above
S s3(1); // direct-initialization from direct init point 1 above
S s4(1, 2); // direct-initialization from direct init point 1 above
int i1{10}; // direct-initialization from direct init point 2 above
int i2(10); // direct-initialization from direct init point 1 above