Alias ​​patterns used in SFINAE lead to hard error

I want to use enabler (alias template enable_if

) defined in one class template in another class template. It looks like this:

template< ... > using enabler = typename std::enable_if< ... >::type;

      

This works great for SFINAE. But when I add another alias pattern in the second class like

template< ... > using enabler
    = typename first_class<..> :: template enabler< ... >;

      

and use this enabler for SFINAE, the substitution (correctly) fails, but with a hard error for g ++ 4.8.0 and 4.8.1. clang ++ 3.4 gives only soft error and SFINAE works.

#include <iostream>
#include <type_traits>

#include <cstdlib>

template< typename T >
class A
{

    struct B {};

public :

    struct U : B {};

    template< typename D, typename R = void >
    using enable_if_is_B = typename std::enable_if< std::is_base_of< B, D >::value, R >::type;

    template< typename D, typename R = void >
    using enable_if_is_not_B = typename std::enable_if< !std::is_base_of< B, D >::value, R >::type;

    template< typename D >
    auto
    operator () (D const &) const
    -> enable_if_is_B< D >
    {
        std::cout << "D is B" << std::endl;
    }

    template< typename D >
    auto
    operator () (D const &) const
    -> enable_if_is_not_B< D >
    {
        std::cout << "D is not B" << std::endl;
    }

};

template< typename T >
struct B
{

    using A_type = A< T >;

    template< typename D, typename R = void >
    using enable_if_is_B = typename A_type::template enable_if_is_B< D, R >;

    template< typename D, typename R = void >
    using enable_if_is_not_B = typename A_type::template enable_if_is_not_B< D, R >;

    template< typename D >
    auto
    operator () (D const &) const
    -> enable_if_is_B< D >
    {
        std::cout << "D is B!" << std::endl;
    }

    template< typename D >
    auto
    operator () (D const &) const
    -> enable_if_is_not_B< D >
    {
        std::cout << "D is not B!" << std::endl;
    }

};

int main()
{
    using T = struct Z;
    using A_type = A< T >;
    using U = typename A_type::U;
#if 0
    using X = A< T >;
#else
    using X = B< T >;
#endif
    X const x;
    x(0);
    x(U());
    return EXIT_SUCCESS;
}

      

which c #if 1

produces:

D is not B
D is B

      

But for using X = B< T >;

g + 4.8.0 an error is generated:

<stdin>: In substitution of 'template<class T> template<class D, class R> using enable_if_is_B = typename std::enable_if<std::is_base_of<A<T>::B, D>::value, R>::type [with D = int; R = void; T = main()::Z]':
<stdin>:48:73:   required by substitution of 'template<class T> template<class D, class R> using enable_if_is_B = typename B<T>::A_type::enable_if_is_B<D, R> [with D = int; R = void; T = main()::Z]'
<stdin>:55:2:   required by substitution of 'template<class D> B<T>::enable_if_is_B<D> B<T>::operator()(const D&) const [with D = D; T = main()::Z] [with D = int]'
<stdin>:82:5:   required from here
<stdin>:18:91: error: no type named 'type' in 'struct std::enable_if<false, void>'
<stdin>: In substitution of 'template<class T> template<class D, class R> using enable_if_is_not_B = typename std::enable_if<(! std::is_base_of<A<T>::B, D>::value), R>::type [with D = A<main()::Z>::U; R = void; T = main()::Z]':
<stdin>:51:81:   required by substitution of 'template<class T> template<class D, class R> using enable_if_is_not_B = typename B<T>::A_type::enable_if_is_not_B<D, R> [with D = A<main()::Z>::U; R = void; T = main()::Z]'
<stdin>:63:2:   required by substitution of 'template<class D> B<T>::enable_if_is_not_B<D> B<T>::operator()(const D&) const [with D = D; T = main()::Z] [with D = A<main()::Z>::U]'
<stdin>:83:7:   required from here
<stdin>:21:96: error: no type named 'type' in 'struct std::enable_if<false, void>'

      

How do I "export / import" a facility from one class template to another?

Why isn't SFINAE working?

+1


source to share





All Articles