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

Deduce the return type of queue::emplace and stack::emplace #4980

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion stl/inc/queue
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public:
#endif // _HAS_CXX23

template <class... _Valty>
_CONTAINER_EMPLACE_RETURN emplace(_Valty&&... _Val) {
_ADAPTOR_EMPLACE_RETURN emplace(_Valty&&... _Val) {
#if _HAS_CXX17
return c.emplace_back(_STD forward<_Valty>(_Val)...);
#else // ^^^ _HAS_CXX17 / !_HAS_CXX17 vvv
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
2 changes: 1 addition & 1 deletion stl/inc/stack
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public:
#endif // _HAS_CXX23

template <class... _Valty>
_CONTAINER_EMPLACE_RETURN emplace(_Valty&&... _Val) {
_ADAPTOR_EMPLACE_RETURN emplace(_Valty&&... _Val) {
#if _HAS_CXX17
return c.emplace_back(_STD forward<_Valty>(_Val)...);
#else // ^^^ _HAS_CXX17 / !_HAS_CXX17 vvv
Expand Down
6 changes: 6 additions & 0 deletions stl/inc/xmemory
Original file line number Diff line number Diff line change
Expand Up @@ -2643,6 +2643,12 @@ _NODISCARD _Elem* _UIntegral_to_buff(_Elem* _RNext, _UTy _UVal) { // used by bot
}
_STD_END

#if _HAS_CXX17
#define _ADAPTOR_EMPLACE_RETURN decltype(auto)
#else // ^^^ _HAS_CXX17 ^^^ / vvv !_HAS_CXX17 vvv
#define _ADAPTOR_EMPLACE_RETURN void
#endif // ^^^ !_HAS_CXX17 ^^^

#if _HAS_CXX17
#define _CONTAINER_EMPLACE_RETURN reference
#else // ^^^ _HAS_CXX17 ^^^ / vvv !_HAS_CXX17 vvv
Expand Down
13 changes: 3 additions & 10 deletions tests/std/tests/VSO_2252142_wrong_C5046/test.compile.pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#include <deque>
#include <forward_list>
#include <list>
#include <queue>
#include <stack>
#include <type_traits>
#include <utility>
#include <vector>
Expand All @@ -21,12 +19,6 @@ struct convertible_to_any {
operator T() &&; // not defined, only used in unevaluated context
};

template <class Cont, class = void>
constexpr bool has_emplace = false;
template <class Cont>
constexpr bool has_emplace<Cont,
void_t<decltype(declval<Cont&>().emplace(declval<convertible_to_any<typename Cont::value_type>>()))>> = true;

template <class Cont, class = void>
constexpr bool has_emplace_back = false;
template <class Cont>
Expand All @@ -51,6 +43,7 @@ STATIC_ASSERT(has_emplace_front<deque<S2>>);
STATIC_ASSERT(has_emplace_front<forward_list<S2>>);
STATIC_ASSERT(has_emplace_back<list<S2>>);
STATIC_ASSERT(has_emplace_front<list<S2>>);
STATIC_ASSERT(has_emplace<queue<S2>>);
STATIC_ASSERT(has_emplace<stack<S2>>);
STATIC_ASSERT(has_emplace_back<vector<bool>>); // Cannot trigger this bug, but for consistency

// N4988 [queue.defn] and [stack.defn] require the container adaptors to have `decltype(auto) emplace(Args&&... args)`,
// allowing them to adapt both C++14-era and C++17-era containers.