Ale*_*der 1 c++ constructor c++11
C++ 11标准中的哪个子句允许我在下面A的return语句中消除它A::operator-()?换句话说,如果我return A{-a.i, -a.j};通过return {-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';
}
Run Code Online (Sandbox Code Playgroud)
n. *_* m. 10
6.6.3/2
带有braced-init-list的return语句通过copy-list-initialization(8.5.4)从指定的初始化列表初始化要从函数返回的对象或引用.[ 例如:
Run Code Online (Sandbox Code Playgroud)std::pair<std::string,int> f(const char* p, int x) { return {p,x}; }- 结束例子 ]
| 归档时间: |
|
| 查看次数: |
114 次 |
| 最近记录: |