Stroustrup C++ 4th Ed Page 796 states that "If Enable_if’s condition evaluates to false, the whole function declaration of which it is part is completely ignored." and "...we don’t declare anything.".
Does anyone know why the first f0()
is being declared? Am I using enable_if
correctly?
My goal is to disable one of the declarations. I'm not sure how both f0()
can have a return type, as the false
version should be missing ::type
.
#include <type_traits>
using namespace std;
template<bool B, typename T>
using Enable_if = typename std::enable_if<B,T>::type;
template <class T>
class X {
Enable_if<false, T> f0(int x) {};
Enable_if<true, T> f0(int x) {};
};
int main(void)
{
X<void> xx;
return 0;
}
Compilation:
clang++ -std=c++11 -Wall -pedantic test197.cc && ./a.out
test197.cc:10:24: error: functions that differ only in their return type cannot
be overloaded
Enable_if<true, T> f0(int x) {};
~~~~~~~~~~~~~~~~~~ ^
test197.cc:9:25: note: previous definition is here
Enable_if<false, T> f0(int x) {};
~~~~~~~~~~~~~~~~~~~ ^
1 error generated.