On Tue, Nov 18, 2025 at 9:44 PM Yongwei Wu via Std-Discussion <std-discussion@lists.isocpp.org> wrote:
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.

See https://eel.is/c++draft/class.temporary#3
 

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


--
Brian Bi