Specialize C ++ member function based on template template argument

I have a class with a template parameter that should decide which of the two data styles it contains. Based on this parameter, I want to implement a member function in one of two different ways. I tried using Boost Enable-If, but to no avail. Here the version of the code that I am most surprised at is not working:

#include <boost/utility/enable_if.hpp>
enum PadSide { Left, Right };
template <int> struct dummy { dummy(int) {} };

template <PadSide Pad>
struct String
{
    typename boost::enable_if_c<Pad ==  Left, void>::type
        getRange(dummy<0> = 0) {}
    typename boost::enable_if_c<Pad == Right, void>::type
        getRange(dummy<1> = 0) {}
};

int main()
{
    String<Left> field;
    field.getRange();
}

To do this, g ++ 4.6.0 says:

no type named ‘typein ‘struct boost::enable_if_c<false, void>’

Of course, the second overload is not supposed to work, but it should be ignored due to SFINAE. If I remove the parameters of the dummy function, g ++ says the following:

typename boost::enable_if_c<(Pad == Right), void>::type
    String<Pad>::getRange()‘
cannot be overloaded withtypename boost::enable_if_c<(Pad == Left), void>::type
    String<Pad>::getRange()‘

That's why I put dummy parameters there - according to the section "Compiler workarounds" in the documentation .

, getRange() Pad. , Enable-If , ( ).

+5
1

getRange() , - struct String PadSide. , "", , , , .

template<PadSide Pad>
struct String
{
    void getRange();
};

template<>
void String<Right>::getRange() { /*....*/ }

template<>
void String<Left>::getRange() { /*....*/ }
+7

All Articles