的元組的深度複製糾正我,如果我錯了,但運行實現指針
std::tuple<double*, bool*> t(new double(3.5), new bool(true));
print_tuple(t);
std::tuple<double*, bool*> n = t;
print_tuple(n);
,當我得到
std::get<0>(t) = 0x1f13d0
std::get<1>(t) = 0x1f13b0
std::get<0>(n) = 0x1f13d0
std::get<1>(n) = 0x1f13b0
這意味着在元組的指針是淺拷貝只,對?所以我寫了一個簡單的工具,旨在深副本是指向一個元組的所有元素:
template <std::size_t N, typename Tuple>
std::enable_if_t<std::is_pointer<std::tuple_element_t<N, Tuple>>::value> assign (Tuple& tuple, const Tuple& other) {
std::get<N>(tuple) = new std::remove_pointer_t<std::tuple_element_t<N, Tuple>>(*std::get<N>(other));
}
template <std::size_t N, typename Tuple>
std::enable_if_t<!std::is_pointer<std::tuple_element_t<N, Tuple>>::value> assign (Tuple& tuple, const Tuple& other) {
std::get<N>(tuple) = std::get<N>(other);
}
template <typename Tuple, std::size_t... Is>
Tuple deep_copy_impl (const Tuple& other, std::index_sequence<Is...>) {
Tuple tuple = {};
const int a[] = {(assign<Is>(tuple, other), 0)...};
static_cast<void>(a);
return tuple;
}
template <typename Tuple>
Tuple deep_copy (const Tuple& other) {
return deep_copy_impl(other, std::make_index_sequence<std::tuple_size<Tuple>::value>{});
}
這似乎爲例子做工精細以上,但是當我的元組tup
嘗試通過
std::tuple<double*, bool*> t(new double(3.5), new bool(true));
std::tuple<int*, std::tuple<double*, bool*>*, char> tup(new int(5), &t, 'a');
我得到了元組指針的深層拷貝,但是元組指針內的指針又被淺拷貝了。我希望這些指針也能被深度複製。如何解決這個問題的任何數量的嵌套指針?下面是我的測試結果:
#include <iostream>
#include <type_traits>
#include <utility>
#include <tuple>
template <std::size_t N, typename Tuple>
std::enable_if_t<std::is_pointer<std::tuple_element_t<N, Tuple>>::value> assign (Tuple& tuple, const Tuple& other) {
std::get<N>(tuple) = new std::remove_pointer_t<std::tuple_element_t<N, Tuple>>(*std::get<N>(other));
}
template <std::size_t N, typename Tuple>
std::enable_if_t<!std::is_pointer<std::tuple_element_t<N, Tuple>>::value> assign (Tuple& tuple, const Tuple& other) {
std::get<N>(tuple) = std::get<N>(other);
}
template <typename Tuple, std::size_t... Is>
Tuple deep_copy_impl (const Tuple& other, std::index_sequence<Is...>) {
Tuple tuple = {};
const int a[] = {(assign<Is>(tuple, other), 0)...};
static_cast<void>(a);
return tuple;
}
template <typename Tuple>
Tuple deep_copy (const Tuple& other) {
return deep_copy_impl(other, std::make_index_sequence<std::tuple_size<Tuple>::value>{});
}
// Testing
template <typename Tuple, std::size_t... Is>
std::ostream& print_tuple_impl (const Tuple& tuple, std::ostream& os, std::index_sequence<Is...>) {
const int a[] = {(os << "std::get<" << Is << ">(tuple) = " << std::get<Is>(tuple) << '\n', 0)...};
static_cast<void>(a);
return os;
}
template <typename Tuple>
std::ostream& print_tuple (const Tuple& tuple, std::ostream& os = std::cout) {
return print_tuple_impl (tuple, os, std::make_index_sequence<std::tuple_size<Tuple>::value>{});
}
int main() {
std::tuple<double*, bool*> t(new double(3.5), new bool(true));
print_tuple(t);
std::tuple<double*, bool*> n = t;
print_tuple(n);
std::cout << "Above is shallow copying only.\n\n";
std::tuple<int*, std::tuple<double*, bool*>*, char> tup(new int(5), &t, 'a');
print_tuple(tup);
std::tuple<int*, std::tuple<double*, bool*>*, char> q = deep_copy(tup);
print_tuple(q);
std::cout << "\nAbove seems like a deep copy, but look at this:\n";
print_tuple(*std::get<1>(tup));
print_tuple(*std::get<1>(q));
}
輸出:
std::get<0>(tuple) = 0x1f13d0
std::get<1>(tuple) = 0x1f13b0
std::get<0>(tuple) = 0x1f13d0
std::get<1>(tuple) = 0x1f13b0
Above is shallow copying only.
std::get<0>(tuple) = 0x1f13f0
std::get<1>(tuple) = 0x72fe10
std::get<2>(tuple) = a
std::get<0>(tuple) = 0x1f1410
std::get<1>(tuple) = 0x1f1430
std::get<2>(tuple) = a
Above seems like a deep copy, but look at this:
std::get<0>(tuple) = 0x1f13d0
std::get<1>(tuple) = 0x1f13b0
std::get<0>(tuple) = 0x1f13d0
std::get<1>(tuple) = 0x1f13b0
我對此並不太熟悉,但是我懷疑如果當前的元組元素本身是一個元組,則需要遞歸調用'deep_copy_impl'中的'depp_copy'。 –
啊!我會嘗試。這可能需要部分專業化的結構。 – prestokeys
確實發佈了答案,如果你想出來的話!我很好奇,看看它是如何擺出來的;) 其實T.C.解決方案看起來很優雅 –