Hi chris,
chris said:
I am currently writing a templated function and want it to be able to
implement it differently based on if the input class has implemented an
overload of swap. However, I can't figure out how I can test for such a
condition, although it seems like a reasonably sensible thing to be able
to do..
I would suggest that you inherit all classes, which can swap from a kind of
compile time interface class
CSwap. Then use a bit of lovely code from Loki (Andrei Alexandrescu),
"Loki::SuperSubclass<CSwap, T>::value". I can particularily recommend the
book
"modern C++ design" from Andrei Alexandrescu, where the background of the
code used within Loki is explained in detail.
Please see the snip of code below. (I tried it with VC7 13.10.3052)
Best Regards
Michael
<snip>
#include "TypeManip.h" // From Loki
// Compiletime interface class fro Swap.
class CSwap{
public:
void Swap();
};
// Swapable class (public interits from CSwap)
class A : public CSwap{
public:
void Test(){
printf("A::Test\n");
}
void Swap(){
printf("\nPerfrom swap...\n");
}
};
// Not swapable class.
class B{
public:
void Test(){
printf("B::Test\n");
}
};
// Template function which performs action an elements of a given type T.
template<class T>
void test(T& val){
// The template Loki::SuperSubclass<...> is the magic from Andrei
Alexandrescu's Loki
testImpl<T, Loki::SuperSubclass<CSwap, T>::value>::action(val);
}
// Use class template as partial specialization of function is not possible.
// Implementation with no swap.
template<class T, bool swap_on>
struct testImpl{
static void action(T& val){
printf("No swap");
}
};
// Implementation with swap. (specialize fro swap_on=true)
template<class T>
struct testImpl<T, true>{
static void action(T& val){
printf("swap");
val.Swap();
}
};
int main(int argc, char* argv[]){
printf("call test with class A (swapable)\n");
test(A());
printf("\n");
printf("call test with class B (NOT swapable)\n");
test(B());
}
</snip>