Search code examples
c++sfinae

"Failed to specialize alias template" errors for the most simple SFINAE bool condition


I'm trying to implement simple condinional implementation and failing tremendously... A tried this:

template<class T, bool COND=0> class A
{
public:
    template< typename TT=T >
        std::enable_if_t<!COND> METHOD() const { };

    template< typename TT=T >
        std::enable_if_t<COND> METHOD() const { };
};

and this:

template<class T, bool COND=0> class A
{
public:
    template< typename TT=T, std::enable_if_t<!COND, int> = 0 >
        void METHOD() const { };

    template< typename TT=T, std::enable_if_t<COND, int> = 0 >
        void METHOD() const { };
};

and quite a few others... and always get "Failed to specialize alias template". What am I doing wrong?

EDIT: Using newest MSVC 2022, just A<int, 0> will trigger the error.


Solution

  • What about as follows?

    template<class T, bool COND=0> class A
    {
    public:
        template< bool CC=COND >
            std::enable_if_t<!CC> METHOD() const { };
    
        template< bool CC=COND >
            std::enable_if_t<CC> METHOD() const { };
    };
    

    I mean... if you want enable/disable a method of a class through std::enable_if, you have to check a test that depend from a template (type or value) of the method, not of the class.

    So

    std::enable_if_t<!COND> 
    

    doesn't works because COND is a template value of the class; you have to use a template value of the method, so you can add a template value CC, that you can default to COND

    template< bool CC=COND >
    

    and check CC instead of COND

    std::enable_if_t<!CC>