C++ Logo

std-discussion

Advanced search

Re: Temporary object address shift?

From: Brian Bi <bbi5291_at_[hidden]>
Date: Tue, 18 Nov 2025 21:45:44 -0500
On Tue, Nov 18, 2025 at 9:44 PM Yongwei Wu via Std-Discussion <
std-discussion_at_[hidden]> 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_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion
>


-- 
*Brian Bi*

Received on 2025-11-19 02:46:02