SFINAE中使用的别名模板导致硬错误

时间:2013-10-15 10:21:19

标签: c++ typedef sfinae typetraits class-template

我想在另一个类模板中使用在一个类模板中定义的启用程序(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不起作用?

0 个答案:

没有答案