Date: Wed, 19 Nov 2025 10:44:30 +0800
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.)
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/
Received on 2025-11-19 02:44:43
