1

我想enable_if在另一个类模板中使用在一个类模板中定义的启用程序(的别名模板)。它看起来像这样:

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

这适用于 SFINAE。但是当我在第二类中添加另一个别名模板时

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

并将此启用程序用于 SFINAE,替换(正确)失败但对于 g++4.8.0 和 4.8.1 出现硬错误。clang++3.4 只给出一个软错误并且 SFINAE 有效。

#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;
}

#if 1产生:

D is not B
D is B

但是对于using X = B< T >;在 g+4.8.0 上生成错误:

<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>'

如何将启动器从一个类模板“导出/导入”到另一个?

为什么 SFINAE 不起作用?

4

0 回答 0