Why not boost :: enable_if causes a duplicate method compilation error

I have code for enable_if, and it allows me to do some awesome things, but I thought it would cause an error, since my two methods shown below have the same method signature.

Does 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;
}
+5
source share
3 answers

my two methods shown below have the same method signature

Look carefully:

… enable_if< boost …

against

… enable_if< ! boost …

They are not the same; they are opposites. If it is disabled, the other is activated. This ensures that exactly one of them is always displayed to the caller. (Remember, enable_ifmakes an ad completely invisible if the condition is false.)

+5
source

do , . , .

, enable_if , , - , (SFINAE: " Error" ), , .

, , .

0

These two methods differ in return type, for example, in the template specialization enable_if.

-1
source

All Articles