Skip to content

Commit 44927a5

Browse files
author
Jimmy Jia
committed
Construct array elements from tuples in-place
For #57
1 parent ab43b70 commit 44927a5

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

cppwamp/include/cppwamp/internal/varianttuple.ipp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,20 @@ using EnableIfTupleEnd =
2727

2828
//------------------------------------------------------------------------------
2929
template <typename T>
30-
void assignFromTupleElement(Variant& v, T&& elem)
30+
T&& forwardTupleElement(T&& elem)
3131
{
3232
static_assert(ArgTraits<T>::isValid,
3333
"wamp::fromTuple - Invalid tuple element type");
34-
v = std::move(elem);
34+
// Normally this should be std::forward, but this function is only called by
35+
// assignFromTuple below, so elem is always actually an rvalue reference, so
36+
// using std::move is sufficient.
37+
return std::move(elem);
3538
}
3639

3740
template <typename... Ts>
38-
void assignFromTupleElement(Variant& v, std::tuple<Ts...>&& tuple)
41+
Array forwardTupleElement(std::tuple<Ts...>&& tuple)
3942
{
40-
v = toArray(std::move(tuple));
43+
return toArray(std::move(tuple));
4144
}
4245

4346
template<std::size_t N = 0, typename... Ts, EnableIfTupleEnd<N, Ts...> = 0>
@@ -46,8 +49,7 @@ void assignFromTuple(Array&, std::tuple<Ts...>&&) {}
4649
template<std::size_t N = 0, typename... Ts, EnableIfTupleElement<N, Ts...> = 0>
4750
void assignFromTuple(Array& array, std::tuple<Ts...>&& tuple)
4851
{
49-
array.emplace_back();
50-
assignFromTupleElement(array.back(), std::get<N>(std::move(tuple)));
52+
array.emplace_back(forwardTupleElement(std::get<N>(std::move(tuple))));
5153
assignFromTuple<N+1, Ts...>(array, std::move(tuple));
5254
}
5355

test/varianttesttuple.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ SCENARIO( "Variant initialization from a tuple", "[Variant]" )
2020
GIVEN( "a tuple of valid types" )
2121
{
2222
auto tuple = std::make_tuple(null, false, true, 0u, -1, 42.0, "foo",
23-
Array{"a", 123}, Object{{"o", 321}});
23+
Array{"a", 123}, Object{{"o", 321}},
24+
std::make_tuple("b", 124));
2425
Variant expected = Array{null, false, true, 0u, -1, 42.0, "foo",
25-
Array{"a", 123}, Object{{"o", 321}}};
26+
Array{"a", 123}, Object{{"o", 321}},
27+
Array{"b", 124}};
2628
WHEN( "a variant is constructed with the tuple" )
2729
{
2830
Variant v(toArray(tuple));

0 commit comments

Comments
 (0)