From e9367246b57652c3a879822b9917873dde46b26c Mon Sep 17 00:00:00 2001 From: q66 Date: Tue, 26 May 2015 21:28:12 +0100 Subject: [PATCH] completely genericized octa::swap (no need for global overloads ever) --- octa/array.h | 5 ----- octa/functional.h | 5 ----- octa/utility.h | 27 +++++++++++++++++++++------ octa/vector.h | 5 ----- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/octa/array.h b/octa/array.h index 7931b3f..1620f5f 100644 --- a/octa/array.h +++ b/octa/array.h @@ -62,11 +62,6 @@ namespace octa { T p_buf[(N > 0) ? N : 1]; }; - - template - void swap(Array &a, Array &b) { - a.swap(b); - } } #endif \ No newline at end of file diff --git a/octa/functional.h b/octa/functional.h index d0dd6ef..67ae35d 100644 --- a/octa/functional.h +++ b/octa/functional.h @@ -617,11 +617,6 @@ namespace octa { } }; - template - void swap(Function &a, Function &b) { - a.swap(b); - } - template bool operator==(nullptr_t, const Function &rhs) { return !rhs; } diff --git a/octa/utility.h b/octa/utility.h index de78ecb..dfcaaaa 100644 --- a/octa/utility.h +++ b/octa/utility.h @@ -36,12 +36,32 @@ namespace octa { /* swap */ - template void swap(T &a, T &b) { + template + struct __OctaSwapTest { + template struct __OctaTest {}; + template static char __octa_test(__OctaTest *); + template static int __octa_test(...); + static constexpr bool value = (sizeof(__octa_test(0)) == sizeof(char)); + }; + + template inline void __octa_swap(T &a, T &b, EnableIf< + __OctaSwapTest::value, bool + > = true) { + a.swap(b); + } + + template inline void __octa_swap(T &a, T &b, EnableIf< + !__OctaSwapTest::value, bool + > = true) { T c(move(a)); a = move(b); b = move(c); } + template void swap(T &a, T &b) { + __octa_swap(a, b); + } + template void swap(T (&a)[N], T (&b)[N]) { for (size_t i = 0; i < N; ++i) { swap(a[i], b[i]); @@ -103,11 +123,6 @@ namespace octa { octa::swap(second, v.second); } }; - - template - void swap(Pair &a, Pair &b) { - a.swap(b); - } } #endif \ No newline at end of file diff --git a/octa/vector.h b/octa/vector.h index 300e468..aa62aac 100644 --- a/octa/vector.h +++ b/octa/vector.h @@ -311,11 +311,6 @@ namespace octa { octa::swap(p_buf, v.p_buf); } }; - - template - void swap(Vector &a, Vector &b) { - a.swap(b); - } } #endif \ No newline at end of file