Somebody encountered a strange case, which made me interested. It can
be simplified to the following code:
#include <iostream>
using namespace std;
struct B {
B() { cout << this << endl; }
//B(const B&) = delete;
//B(const B&) {}
};
B get()
{
return B{};
}
int main()
{
B b = get();
cout << &b << endl;
}
https://godbolt.org/z/q3xx3xdcn
The code will output two different addresses on Clang and GCC.
However, uncommenting any of the copy-constructor would behave
differently.
Given we have guaranteed copy elision, I am surprised with the result.
Is it a bug of the compiler, or is it allowed behaviour under the
current C++ standard?
(The original case was more complicated, and can be rewritten to work
around the problem, but the root cause was the same. But the problem
itself is very intriguing.)
--
Yongwei Wu
URL: http://wyw.dcweb.cn/
--
Std-Discussion mailing list
Std-Discussion@lists.isocpp.org
https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion