Шаблоны псевдонимов, используемые в SFINAE, приводят к серьезной ошибке

Я хочу использовать активатор (псевдоним шаблона 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 не работает?

1

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector