Skip to content

Commit

Permalink
Deduce the return type of queue::emplace and stack::emplace (micr…
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanTLavavej authored Sep 28, 2024
1 parent bbe8b6b commit d194fcd
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 12 deletions.
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
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 @@ -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
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.

0 comments on commit d194fcd

Please sign in to comment.