From d194fcd5a183118bcbc992c92158faf9016dd3b7 Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Sat, 28 Sep 2024 13:26:14 -0700 Subject: [PATCH] Deduce the return type of `queue::emplace` and `stack::emplace` (#4980) --- stl/inc/queue | 2 +- stl/inc/stack | 2 +- stl/inc/xmemory | 6 ++++++ .../VSO_2252142_wrong_C5046/test.compile.pass.cpp | 13 +++---------- 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/stl/inc/queue b/stl/inc/queue index 9c0262663c..9fc6d300e0 100644 --- a/stl/inc/queue +++ b/stl/inc/queue @@ -135,7 +135,7 @@ public: #endif // _HAS_CXX23 template - _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 diff --git a/stl/inc/stack b/stl/inc/stack index 2640586df5..623640ad34 100644 --- a/stl/inc/stack +++ b/stl/inc/stack @@ -120,7 +120,7 @@ public: #endif // _HAS_CXX23 template - _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 diff --git a/stl/inc/xmemory b/stl/inc/xmemory index 92371e2c4a..dfc3ad3b7e 100644 --- a/stl/inc/xmemory +++ b/stl/inc/xmemory @@ -2639,6 +2639,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 diff --git a/tests/std/tests/VSO_2252142_wrong_C5046/test.compile.pass.cpp b/tests/std/tests/VSO_2252142_wrong_C5046/test.compile.pass.cpp index 8fa63feda4..52462f3aa2 100644 --- a/tests/std/tests/VSO_2252142_wrong_C5046/test.compile.pass.cpp +++ b/tests/std/tests/VSO_2252142_wrong_C5046/test.compile.pass.cpp @@ -4,8 +4,6 @@ #include #include #include -#include -#include #include #include #include @@ -21,12 +19,6 @@ struct convertible_to_any { operator T() &&; // not defined, only used in unevaluated context }; -template -constexpr bool has_emplace = false; -template -constexpr bool has_emplace().emplace(declval>()))>> = true; - template constexpr bool has_emplace_back = false; template @@ -51,6 +43,7 @@ STATIC_ASSERT(has_emplace_front>); STATIC_ASSERT(has_emplace_front>); STATIC_ASSERT(has_emplace_back>); STATIC_ASSERT(has_emplace_front>); -STATIC_ASSERT(has_emplace>); -STATIC_ASSERT(has_emplace>); STATIC_ASSERT(has_emplace_back>); // 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.