C++11 标准中的哪些条款允许我消除下面“A::operator-()”中“返回”语句中的“A”
What clause in the C++11 Standard does allow me to eliminate the `A` in the `return` statement in the `A::operator-()` below?
C++11 标准中的哪个子句允许我消除下面A::operator-()
中return
语句中的A
?换句话说,如果我通过return {-a.i, -a.j};
替换表达式return A{-a.i, -a.j};
,代码将正确编译和执行。如果可能的话,我想知道使用标准是如何工作的?
#include <iostream>
struct A {
int i;
int j;
A(int n, int m) : i(n), j(m) {}
};
A operator-(A a) { return A{-a.i, -a.j}; }
int main()
{
A a(1, 2);
A b = -a;
std::cout << b.i << " " << b.j << 'n';
}
6.6.3/2
带有大括号初始化列表的 return 语句通过指定初始值设定项的复制列表初始化 (8.5.4) 初始化要从函数返回的对象或引用 列表。[ 示例:
std::pair<std::string,int> f(const char* p, int x) { return {p,x}; }
— 结束示例 ]
这在第
8.5.4 节的段落 #3 中描述 C++ 标准的列表初始化
— 否则,如果 T 是类类型,则考虑构造函数。这 枚举适用的构造函数并选择最佳构造函数 通过重载解析(13.3、13.3.1.7)。如果变窄 转换(见下文)是转换任何参数所必需的, 程序格式不正确。
下面结束有一个例子
struct S {
// no initializer-list constructors
S(int, double, double); // #1
S(); // #2
// ...
};
S s1 = { 1, 2, 3.0 }; // OK: invoke #1
S s2 { 1.0, 2, 3 }; // error: narrowing
S s3 { }; // OK: invoke #2
相关文章:
- 我的简单if-else语句是如何无法访问的代码
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- 为什么是0;C++中的有效语句
- Insert函数不适用于2 if语句C++
- If语句未被求值C++
- C++嵌套if语句,基本货币交换
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 是否可以使用if constexpr删除控制流语句
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 我似乎对if/else的基本语句有问题:/
- 为什么 std::optional::operator=(U&&) 要求你是非标量类型?
- 当调用switch语句中的函数时(即使函数不包含循环),似乎是永不结束的循环的问题
- 'operator='已弃用:改用 QDir::setPath()
- If语句在c++中被忽略
- 比较if语句中的数组值和int值
- 以在Qt中的IF语句中设置时间延迟
- 过载'operator new'如何导致无限循环?
- C++11 标准中的哪些条款允许我消除下面“A::operator-()”中“返回”语句中的“A”
- 在switch语句中使用operator关键字