【发布时间】:2018-03-02 16:11:54
【问题描述】:
有没有人想解释一下为什么案例 1 和案例 2 对这段代码 sn-p 有不同的输出。
struct A {
A() { cout << "A()" << endl; }
A(int i) { cout << "A(int)" << endl; }
A(const A&) { cout << "A(const A&)" << endl; }
A(A&&) noexcept { cout << "A(A&&)" << endl; }
A& operator=(const A&) { cout << "operator=(const A&)" << endl; return *this; }
A& operator=(A&&) noexcept { cout << "operator=(A&&)" << endl; return *this; }
friend bool operator< (const A&, const A&) { return true; }
};
int main() {
std::set<A> aSet;
aSet.insert(1); // case 1
//aSet.insert({1}); // case 2
return 0;
}
对于情况 1,输出为:
A(int)
A(A&&)
对于情况 2 是:
A(int)
A(const A&)
编译器版本为:
g++ --version g++-7 (SUSE Linux) 7.2.1 20170901 [gcc-7-branch 修订版 251580] 版权所有 (C) 2017 Free Software Foundation, Inc.
【问题讨论】:
标签: c++ c++11 list-initialization