Ale*_* B. 4 c++ tuples std visual-studio-2010
我在stl元组的帮助下为一些变量实现了保存/恢复功能,如下所示:
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;
这段代码效果很好.但不是明确指定元组成员类型
std::tuple<double, double, int> saved = tupleRef;
我想宁愿删除所有tupleRef成员的引用,如下所示
auto saved = remove_ref_from_tuple_members(tupleRef);
我相信可以为此编写"remove_ref_from_tuple_members"模板.
谢谢你的回答.
可以使用简单类型别名来应用std::remove_reference元组中的所有类型.
template <typename... T>
using tuple_with_removed_refs = std::tuple<typename std::remove_reference<T>::type...>;
有了这个,您现在可以编写功能模板:
template <typename... T>
tuple_with_removed_refs remove_ref_from_tuple_members(std::tuple<T...> const& t) {
    return tuple_with_removed_refs { t };
}