lundi 26 mars 2018

How to have a member function implementation dependent on class' template parameter?

This is my best attempt:

#include <iostream>

template <bool EnableSomething = false>
class MyClass
{
    typename std::enable_if< EnableSomething >::type
        something(int& x)
    {
        x += 1; //do something
    }

    typename std::enable_if< !EnableSomething >::type
        something(int& x)
    {
        // do nothing, should be optimized away
    }

public:
    void Process()
    {
        int x = 0;
        something(x);
        std::cout << "Enabled: " << EnableSomething << ".  x = " << x << std::endl;
    }
};

int main()
{
    MyClass<true> yes;
    MyClass<false> no;
    yes.Process();
    no.Process();
    return 0;
}

The compiler says: tester.cpp(12): error C2039: 'type': is not a member of 'std::enable_if<false,_Ty>'

Aucun commentaire:

Enregistrer un commentaire