0

Here is the code that compilable and works as intended. (Thank juanchopanza)

First version (correct)

#include <iostream>
#include <string>
using namespace std;

enum EN{ EN1,EN2 };
template<EN T1> class B{
    //#BLOCK
    public: template<EN enLocal=T1> typename  
      std::enable_if<enLocal==EN1,void>::type test(){  //<-- not cute

        std::cout<<"EN1"<<std::endl;   
    }
    public: template<EN enLocal=T1> typename   
      std::enable_if<enLocal==EN2,void>::type test(){  //<-- not cute

        std::cout<<"EN2"<<std::endl;   
    }
    //#END BLOCK
};

int main() {
    B<EN1> b;
    b.test(); //should print "EN1"
    return 0;
}

I think the code is hard to read and not cute.

Thus, I tried improve readability the #BLOCK to the below code, but it is no longer compilable.
Why? ... I think this second version is far easier to understand for me.

Second version (uncompilable)

//#BLOCK
public: typename std::enable_if<T1==EN1,void>::type test(){  //<-- cute
    std::cout<<"EN1"<<std::endl;   
}
public: typename std::enable_if<T1==EN2,void>::type test(){  //<-- cute
   //^ error: functions that differ only in their return type cannot be overloaded
    std::cout<<"EN2"<<std::endl;   
}
//#END BLOCK

More specifically, which C++ language specification that makes the second version uncompilable?

Why can't I put T1 inside std::enable_if<>?

I am new to C++.

javaLover
  • 6,347
  • 2
  • 22
  • 67

0 Answers0