Множественная условная специализация шаблона c ++ - PullRequest
2 голосов
/ 06 мая 2019

У меня есть структура

template <typename A, typename B>
struct foo {static const int type = 0;};

Я хочу, чтобы реализация отличалась, когда A и B являются арифметическими. Однако это не сработало.

template <typename A, typename B, typename std::enable_if<
        std::is_arithmetic<A>::value && 
        std::is_arithmetic<B>::value, bool>::type = 0>
struct foo<A, B> {static const int type = 1;}

Я получаю ошибку компилятора default template arguments may not be used in partial specializations. Так как я могу сделать эту работу?

Следует иметь в виду, что я также хочу иметь другие более простые специализации шаблонов, которые могут выглядеть следующим образом.

template <>
struct foo<string, vector<int>> {static const int type = 2;}

Таким образом, я хочу, чтобы первое определение было похоже на значение по умолчанию, а затем определил группу специализированных реализаций, одна из которых является общей.

1 Ответ

3 голосов
/ 06 мая 2019

Вы можете определить основной шаблон , частичная специализация и полная специализация отдельно, например,

// the primary template
template <typename A, typename B, typename = void>
struct foo {static const int type = 0;};

// the partial specialization
template <typename A, typename B>
struct foo<A, B, typename std::enable_if<
        std::is_arithmetic<A>::value && 
        std::is_arithmetic<B>::value>::type> {static const int type = 1;};

// the full specialization
template <>
struct foo<string, vector<int>, void> {static const int type = 2;};

LIVE

...