Skip to content

[libc++] Add nodiscard attribute to std::make_unique/std::make_shared #153115

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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

lukasx999
Copy link

@lukasx999 lukasx999 commented Aug 12, 2025

I think nodiscard makes a lot of sense as a sensible default for smart pointer factory functions.
Calling make_unique and discarding the return value, will lead to an unnecessary allocation and therefore is a misuse of the function. (see https://libcxx.llvm.org/CodingGuidelines.html#apply-nodiscard-where-relevant)
Im kind of surprised that this isn't the default in libc++, aswell as libstdc++, since the standard says that nodiscard attributes are implementation defined.

@lukasx999 lukasx999 requested a review from a team as a code owner August 12, 2025 01:12
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Aug 12, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 12, 2025

@llvm/pr-subscribers-libcxx

Author: Lukas (lukasx999)

Changes

I think nodiscard makes a lot of sense as a sensible default for smart pointer factory functions.
Im kind of surprised that this isn't the default in libc++, aswell as libstdc++, since the standard says that nodiscard attributes are implementation defined.


Full diff: https://github.com/llvm/llvm-project/pull/153115.diff

1 Files Affected:

  • (modified) libcxx/include/memory (+4-4)
diff --git a/libcxx/include/memory b/libcxx/include/memory
index ca880c83a544d..5a4100155fc94 100644
--- a/libcxx/include/memory
+++ b/libcxx/include/memory
@@ -566,15 +566,15 @@ class bad_weak_ptr
 };
 
 template<class T, class... Args>
-constexpr unique_ptr<T> make_unique(Args&&... args);                            // C++14, constexpr since C++23
+[[nodiscard]] constexpr unique_ptr<T> make_unique(Args&&... args);                            // C++14, constexpr since C++23
 template<class T>
-constexpr unique_ptr<T> make_unique(size_t n);                                  // C++14, constexpr since C++23
+[[nodiscard]] constexpr unique_ptr<T> make_unique(size_t n);                                  // C++14, constexpr since C++23
 template<class T, class... Args> unspecified   make_unique(Args&&...) = delete; // C++14, T == U[N]
 
 template<class T>
-  constexpr unique_ptr<T> make_unique_for_overwrite();                        // T is not array, C++20, constexpr since C++23
+  [[nodiscard]] constexpr unique_ptr<T> make_unique_for_overwrite();                        // T is not array, C++20, constexpr since C++23
 template<class T>
-  constexpr unique_ptr<T> make_unique_for_overwrite(size_t n);                // T is U[], C++20, constexpr since C++23
+  [[nodiscard]] constexpr unique_ptr<T> make_unique_for_overwrite(size_t n);                // T is U[], C++20, constexpr since C++23
 template<class T, class... Args>
   unspecified make_unique_for_overwrite(Args&&...) = delete;                  // T is U[N], C++20
 

@philnik777 philnik777 changed the title added nodiscard attribute to std::make_unique() [libc++] Add nodiscard attribute to std::make_unique() Aug 12, 2025
Copy link
Contributor

@philnik777 philnik777 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is incorrect. You've modified a comment.

@lukasx999
Copy link
Author

lukasx999 commented Aug 12, 2025

Do you mean the whitespace between the function and the comment?

@philnik777
Copy link
Contributor

No, you didn't modify any code. You just modified a comment.

@H-G-Hristov
Copy link
Contributor

You will need to also add tests.

@lukasx999
Copy link
Author

No, you didn't modify any code. You just modified a comment.

Sorry, my editor was not syntax-highlighting the comment - as it should, I just though that was because of some #if version checking, which is very common in libc++.
I updated the actual make_unique and make_shared implementation accordingly.
Do you think the memory synopsis should still contain the nodiscard attribute?

Copy link
Contributor

@philnik777 philnik777 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think the memory synopsis should still contain the nodiscard attribute?

No

Please also add tests. You can take a look at libcxx/test/libcxx/mem/mem.res/nodiscard.verify.cpp for an example.

Copy link
Contributor

@philnik777 philnik777 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you refer in the commit message to https://libcxx.llvm.org/CodingGuidelines.html#apply-nodiscard-where-relevant instead of "I think it makes a lot of sense"?

S() = default;
};

std::make_unique<S>(S{}); // expected-warning {{ignoring return value of function}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you're missing a few test cases. I count 12 new annotations above, but this tests only 6 functions.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where do you see 12 annotations? I only see 6.

@lukasx999 lukasx999 changed the title [libc++] Add nodiscard attribute to std::make_unique() [libc++] Add nodiscard attribute to std::make_unique/std::make_shared Aug 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants