I have got the code for enable_if working and it is allowing me to do some awesome stuff but I thought it would cause an error as my two methods shown below have the same method signature.
Anyone know why this is allowed?
#include <iostream>
#include <boost/type_traits>
template<bool T, class R = void>
struct enable_if{
typedef R type;
};
template<class R>
struct enable_if<false, R>{
};
template<class T>
typename enable_if<boost::is_pod<T>::value >::type print(const T& item){
std::cout << "T is a pod with the value: " << item << std::endl;
}
template<class T>
typename enable_if<!(boost::is_pod<T>::value) >::type print(const T& item){
std::cout << "T is not a pod with the value: " << item << std::endl;
}
int main(int argc, const char * argv[])
{
print(1);
return 0;
}
my two methods shown below have the same method signature
Look closely:
… enable_if< boost …
vs
… enable_if< ! boost …
They're not the same, they're opposites. If one is disabled, the other is enabled. That guarantees that exactly one is always visible to the caller. (Remember, enable_if
renders the declaration completely invisible if the condition is false.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With