dimanche 26 juillet 2020

How std::enable_if prevents declaration of member template?

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.".

I have also read this suggested thread in which SFINAE only works if substitution in argument deduction of a template argument makes the construct ill-formed.

For this example, I am trying to understand how SFINAE omits the Enable_if<false, T> f0(int x) {}; construct.

Is it because of the lack of a return type ::type makes the template construct ill-formed language-wise?

#include <type_traits>
using namespace std;

template<bool B, typename T>
using Enable_if = typename std::enable_if<B, T>::type;

struct X 
{
   template <class T>
   Enable_if<false, T> f0(int x) {};
   template <class T>
   Enable_if<true, T> f0(int x) {};
};

int main(void)
{
   X xx;
   xx.f0<void>(0);
   return 0;
}

I understand the case in the prior mentioned thread for a construct:

template <typename enable_if<false>::type* = nullptr> 
void f0() {}

this is because the template parameter is ill-formed (no ::type to assign nullptr to.)

Aucun commentaire:

Enregistrer un commentaire