Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LWG4148: unique_ptr::operator* should not allow dangling references #118362

Open
ldionne opened this issue Dec 2, 2024 · 3 comments · May be fixed by #128213
Open

LWG4148: unique_ptr::operator* should not allow dangling references #118362

ldionne opened this issue Dec 2, 2024 · 3 comments · May be fixed by #128213
Assignees
Labels
c++26 libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. lwg-issue this is an issue that was filed to the Library Working Group

Comments

@ldionne
Copy link
Member

ldionne commented Dec 2, 2024

Link: https://wg21.link/LWG4148

@ldionne ldionne added c++26 libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. lwg-issue this is an issue that was filed to the Library Working Group labels Dec 2, 2024
@elhewaty
Copy link
Member

I think this is already implemented: https://godbolt.org/z/Y8KEY5GK9

when I test on my local machine I get this error

./bin/clang++ -std=c++26 -stdlib=libc++  -nostdinc++ -I ./include/c++/v1   -L ./lib/libc++ -Wl,-rpath,./lib p.cpp
In file included from p.cpp:2:
In file included from ./include/c++/v1/iostream:46:
In file included from ./include/c++/v1/istream:170:
In file included from ./include/c++/v1/__ostream/basic_ostream.h:18:
./include/c++/v1/__memory/unique_ptr.h:265:12: error: returning reference to local temporary object
  265 |     return *__ptr_;
      |            ^~~~~~~
p.cpp:15:11: note: in instantiation of member function 'std::unique_ptr<const int, deleter>::operator*' requested here
   15 |   int i = *p; // undefined
      |           ^
1 error generated.

@frederick-vs-ja
Copy link
Contributor

I think this is already implemented: https://godbolt.org/z/Y8KEY5GK9

This is due to C++26 core language change P2748R5 - Disallow Binding a Returned Glvalue to a Temporary.

But the resolution makes sense in C++23. Perhaps we should add static_assert in C++23 (or an even earlier) mode using __reference_converts_from_temporary (as reference_converts_from_temporary_v isn't implemented yet).

@elhewaty
Copy link
Member

Thanks, I added it but the error message is
error: indirection requires pointer operand ('std::unique_ptr<const int, deleter>' invalid)
Is this correct?

I added this to operator*()

requires (!__reference_converts_from_temporary(__add_lvalue_reference_t<_Tp>, decltype(*declval<pointer>())))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++26 libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. lwg-issue this is an issue that was filed to the Library Working Group
Projects
Status: Todo
3 participants