Implement LWG2783: stack::emplace() and queue::emplace() should return decltype(auto)

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@323385 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Marshall Clow
2018-01-24 22:42:25 +00:00
parent 8286acce44
commit cc7048888e
5 changed files with 64 additions and 26 deletions

View File

@@ -290,7 +290,7 @@ public:
template <class... _Args> template <class... _Args>
_LIBCPP_INLINE_VISIBILITY _LIBCPP_INLINE_VISIBILITY
#if _LIBCPP_STD_VER > 14 #if _LIBCPP_STD_VER > 14
reference emplace(_Args&&... __args) decltype(auto) emplace(_Args&&... __args)
{ return c.emplace_back(_VSTD::forward<_Args>(__args)...);} { return c.emplace_back(_VSTD::forward<_Args>(__args)...);}
#else #else
void emplace(_Args&&... __args) void emplace(_Args&&... __args)

View File

@@ -199,7 +199,7 @@ public:
template <class... _Args> template <class... _Args>
_LIBCPP_INLINE_VISIBILITY _LIBCPP_INLINE_VISIBILITY
#if _LIBCPP_STD_VER > 14 #if _LIBCPP_STD_VER > 14
reference emplace(_Args&&... __args) decltype(auto) emplace(_Args&&... __args)
{ return c.emplace_back(_VSTD::forward<_Args>(__args)...);} { return c.emplace_back(_VSTD::forward<_Args>(__args)...);}
#else #else
void emplace(_Args&&... __args) void emplace(_Args&&... __args)

View File

@@ -1,6 +1,6 @@
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// //
// The LLVM Compiler Infrastructure // The LLVM Compiler Infrastructure
// //
// This file is dual licensed under the MIT and the University of Illinois Open // This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details. // Source Licenses. See LICENSE.TXT for details.
@@ -11,36 +11,55 @@
// <queue> // <queue>
// template <class... Args> reference emplace(Args&&... args); // template <class... Args> decltype(auto) emplace(Args&&... args);
// return type is 'reference' in C++17; 'void' before // return type is 'decltype(auto)' in C++17; 'void' before
// whatever the return type of the underlying container's emplace_back() returns.
#include <queue> #include <queue>
#include <cassert> #include <cassert>
#include <list>
#include "test_macros.h" #include "test_macros.h"
#include "../../../Emplaceable.h" #include "../../../Emplaceable.h"
template <typename Queue>
void test_return_type() {
typedef typename Queue::container_type Container;
typedef typename Container::value_type value_type;
typedef decltype(std::declval<Queue>().emplace(std::declval<value_type &>())) queue_return_type;
#if TEST_STD_VER > 14
typedef decltype(std::declval<Container>().emplace_back(std::declval<value_type>())) container_return_type;
static_assert(std::is_same<queue_return_type, container_return_type>::value, "");
#else
static_assert(std::is_same<queue_return_type, void>::value, "");
#endif
}
int main() int main()
{ {
typedef Emplaceable T; test_return_type<std::queue<int> > ();
std::queue<Emplaceable> q; test_return_type<std::queue<int, std::list<int> > > ();
typedef Emplaceable T;
std::queue<Emplaceable> q;
#if TEST_STD_VER > 14 #if TEST_STD_VER > 14
T& r1 = q.emplace(1, 2.5); T& r1 = q.emplace(1, 2.5);
assert(&r1 == &q.back()); assert(&r1 == &q.back());
T& r2 = q.emplace(2, 3.5); T& r2 = q.emplace(2, 3.5);
assert(&r2 == &q.back()); assert(&r2 == &q.back());
T& r3 = q.emplace(3, 4.5); T& r3 = q.emplace(3, 4.5);
assert(&r3 == &q.back()); assert(&r3 == &q.back());
assert(&r1 == &q.front()); assert(&r1 == &q.front());
#else #else
q.emplace(1, 2.5); q.emplace(1, 2.5);
q.emplace(2, 3.5); q.emplace(2, 3.5);
q.emplace(3, 4.5); q.emplace(3, 4.5);
#endif #endif
assert(q.size() == 3); assert(q.size() == 3);
assert(q.front() == Emplaceable(1, 2.5)); assert(q.front() == Emplaceable(1, 2.5));
assert(q.back() == Emplaceable(3, 4.5)); assert(q.back() == Emplaceable(3, 4.5));
} }

View File

@@ -11,20 +11,39 @@
// <stack> // <stack>
// template <class... Args> reference emplace(Args&&... args); // template <class... Args> decltype(auto) emplace(Args&&... args);
// return type is 'reference' in C++17; 'void' before // return type is 'decltype(auto)' in C++17; 'void' before
// whatever the return type of the underlying container's emplace_back() returns.
#include <stack> #include <stack>
#include <cassert> #include <cassert>
#include <vector>
#include "test_macros.h" #include "test_macros.h"
#include "../../../Emplaceable.h" #include "../../../Emplaceable.h"
template <typename Stack>
void test_return_type() {
typedef typename Stack::container_type Container;
typedef typename Container::value_type value_type;
typedef decltype(std::declval<Stack>().emplace(std::declval<value_type &>())) stack_return_type;
#if TEST_STD_VER > 14
typedef decltype(std::declval<Container>().emplace_back(std::declval<value_type>())) container_return_type;
static_assert(std::is_same<stack_return_type, container_return_type>::value, "");
#else
static_assert(std::is_same<stack_return_type, void>::value, "");
#endif
}
int main() int main()
{ {
test_return_type<std::stack<int> > ();
test_return_type<std::stack<int, std::vector<int> > > ();
typedef Emplaceable T; typedef Emplaceable T;
std::stack<Emplaceable> q; std::stack<Emplaceable> q;
#if TEST_STD_VER > 14 #if TEST_STD_VER > 14
T& r1 = q.emplace(1, 2.5); T& r1 = q.emplace(1, 2.5);
assert(&r1 == &q.top()); assert(&r1 == &q.top());

View File

@@ -54,7 +54,7 @@
<!-- <!--
<tr><td><a href="https://wg21.link/n3346">3346</a></td><td>LWG</td><td>Terminology for Container Element Requirements - Rev 1</td><td>Kona</td><td>Complete</td><td>3.4</td></tr> <tr><td><a href="https://wg21.link/n3346">3346</a></td><td>LWG</td><td>Terminology for Container Element Requirements - Rev 1</td><td>Kona</td><td>Complete</td><td>3.4</td></tr>
--> -->
<tr><td><a href="https://wg21.link/P0463R1">P0463R1</a></td><td>LWG</td><td>Endian just Endian</td><td>Toronto</td><td><I>In progress</I></td><td>7.0</td></tr> <tr><td><a href="https://wg21.link/P0463R1">P0463R1</a></td><td>LWG</td><td>Endian just Endian</td><td>Toronto</td><td>Complete</td><td>7.0</td></tr>
<tr><td><a href="https://wg21.link/P0674R1">P0674R1</a></td><td>LWG</td><td>Extending make_shared to Support Arrays</td><td>Toronto</td><td></td><td></td></tr> <tr><td><a href="https://wg21.link/P0674R1">P0674R1</a></td><td>LWG</td><td>Extending make_shared to Support Arrays</td><td>Toronto</td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td></td><td></td></tr> <tr><td></td><td></td><td></td><td></td><td></td><td></td></tr>
@@ -94,7 +94,7 @@
<tr><td><a href="https://wg21.link/LWG2444">2444</a></td><td>Inconsistent complexity for <tt>std::sort_heap</tt></td><td>Toronto</td><td></td></tr> <tr><td><a href="https://wg21.link/LWG2444">2444</a></td><td>Inconsistent complexity for <tt>std::sort_heap</tt></td><td>Toronto</td><td></td></tr>
<tr><td><a href="https://wg21.link/LWG2593">2593</a></td><td>Moved-from state of Allocators</td><td>Toronto</td><td></td></tr> <tr><td><a href="https://wg21.link/LWG2593">2593</a></td><td>Moved-from state of Allocators</td><td>Toronto</td><td></td></tr>
<tr><td><a href="https://wg21.link/LWG2597">2597</a></td><td><tt>std::log</tt> misspecified for complex numbers</td><td>Toronto</td><td></td></tr> <tr><td><a href="https://wg21.link/LWG2597">2597</a></td><td><tt>std::log</tt> misspecified for complex numbers</td><td>Toronto</td><td></td></tr>
<tr><td><a href="https://wg21.link/LWG2783">2783</a></td><td><tt>stack::emplace()</tt> and <tt>queue::emplace()</tt> should return <tt>decltype(auto)</tt></td><td>Toronto</td><td></td></tr> <tr><td><a href="https://wg21.link/LWG2783">2783</a></td><td><tt>stack::emplace()</tt> and <tt>queue::emplace()</tt> should return <tt>decltype(auto)</tt></td><td>Toronto</td><td>Complete</td></tr>
<tr><td><a href="https://wg21.link/LWG2932">2932</a></td><td>Constraints on parallel algorithm implementations are underspecified</td><td>Toronto</td><td></td></tr> <tr><td><a href="https://wg21.link/LWG2932">2932</a></td><td>Constraints on parallel algorithm implementations are underspecified</td><td>Toronto</td><td></td></tr>
<tr><td><a href="https://wg21.link/LWG2937">2937</a></td><td>Is <tt>equivalent("existing_thing", "not_existing_thing")</tt> an error</td><td>Toronto</td><td>Complete</td></tr> <tr><td><a href="https://wg21.link/LWG2937">2937</a></td><td>Is <tt>equivalent("existing_thing", "not_existing_thing")</tt> an error</td><td>Toronto</td><td>Complete</td></tr>
<tr><td><a href="https://wg21.link/LWG2940">2940</a></td><td><tt>result_of</tt> specification also needs a little cleanup</td><td>Toronto</td><td></td></tr> <tr><td><a href="https://wg21.link/LWG2940">2940</a></td><td><tt>result_of</tt> specification also needs a little cleanup</td><td>Toronto</td><td></td></tr>
@@ -135,7 +135,7 @@
<!-- <tr><td></td><td></td><td></td><td></td></tr> --> <!-- <tr><td></td><td></td><td></td><td></td></tr> -->
</table> </table>
<p>Last Updated: 22-Jan-2018</p> <p>Last Updated: 24-Jan-2018</p>
</div> </div>
</body> </body>
</html> </html>