6

I'm implementing save/restore functionality for some variables with the help of stl tuples as follows:

double a = 1, b = 2;
int c = 3;
auto tupleRef = std::make_tuple(std::ref(a), std::ref(b), std::ref(c));

// here I'm saving current state of a, b, c
std::tuple<double, double, int> saved = tupleRef;

//here goes block of code, where a, b, and c get spoiled
......................
//

//now I'm restoring initial state of a, b, c
tupleRef = savedTuple;

This code works well. But instead of explicit specifying tuple members types in

std::tuple<double, double, int> saved = tupleRef;

I'd like to rather remove references from all tupleRef members, like in the following

auto saved = remove_ref_from_tuple_members(tupleRef);

I believe that it is possible to write "remove_ref_from_tuple_members" template to that end.

Thanks for answers.

2 Answers 2

8

A simple type alias can be used to apply std::remove_reference to all types in a tuple.

template <typename... T>
using tuple_with_removed_refs = std::tuple<typename std::remove_reference<T>::type...>;

Armed with this you can now write the function template:

template <typename... T>
tuple_with_removed_refs remove_ref_from_tuple_members(std::tuple<T...> const& t) {
    return tuple_with_removed_refs { t };
}
4
  • 2
    Wait, you mean template parameter packs have a built-in "map" operation (in the functional programming sense, not the std::map sense)? Praise be, that I should live to see such times! Oct 5, 2012 at 9:21
  • @SteveJessop Yes, I think you can say that. You can expand a pack into any pattern. Oct 5, 2012 at 9:34
  • Thanks for the answer. I believe it works, at least looks logical. Unfortunately variadic templates don’t compile on my computer in Visual Studil 2010 nor in VS2012, they are just not supported by VS. It looks like they hard-coded tuple to have max 15 members.
    – Alex B.
    Oct 6, 2012 at 23:30
  • To fix it I had to change both instances of tuple_with_removed_refs in the second function to tuple_with_removed_refs<T...>
    – M.M
    Mar 11, 2018 at 3:40
0

Thanks to R. Martinho Fernandes, whose code I was able to modify to compile in Visual Studio where tuple is hard-coded as a template with 10 types with unused types being empty structs.

#define _RR_(x) typename std::remove_reference<x>::type
#define _no_ref_tuple_  std::tuple<_RR_(T0), _RR_(T1), _RR_(T2), _RR_(T3), _RR_(T4), _RR_(T5), _RR_(T6), _RR_(T7), _RR_(T8), _RR_(T9)>

template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9>
_no_ref_tuple_ map_remove_ref(std::tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> const& t) 
{
    return _no_ref_tuple_(t);
}

I also think that binding of the refs into tuple like in

auto tupleRef = std::make_tuple(std::ref(x_0), ..., std::ref(x_n));

can be made less verbose:

auto tupleRef = std::forward_as_tuple(x_0, ..., x_n);

but this again will not work in VS, as there is no std::forward_as_tuple.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.