Использование CRTP с частичной специализацией класса? - PullRequest
0 голосов
/ 03 июня 2018

Я пытаюсь смешать operator [] с классом.Моя проблема в том, что я частично специализировал класс, и компилятору не нравится, когда я не указываю параметры шаблона для производного класса:

#include <iostream>
#include <type_traits>

using namespace std;

template <typename T>
struct mixin {
    template <typename U>
    void operator[](U u) {
        cout << u;
    }
};


template <typename T, typename = void>
struct derived : mixin<derived> {};


template <typename T>
struct derived<T, 
    typename enable_if<
        is_same<T, int>{}
    >::type> : mixin<derived> {};


int main() {
    derived<int> d;
    d[3.14];
}

С помощью clang это дает:

test.cc:16:24: error: use of class template 'derived' requires template arguments
struct derived : mixin<derived> {};
                       ^~~~~~~
test.cc:16:8: note: template is declared here
struct derived : mixin<derived> {};
       ^
test.cc:23:22: error: use of class template 'derived' requires template arguments
    >::type> : mixin<derived> {};
                     ^~~~~~~
test.cc:16:8: note: template is declared here
struct derived : mixin<derived> {};
       ^

gcc еще менее полезен:

test.cc:16:31: error: type/value mismatch at argument 1 in template parameter list for ‘template<class T> struct mixin’
 struct derived : mixin<derived> {};
                               ^
test.cc:16:31: note:   expected a type, got ‘derived’
test.cc:23:29: error: type/value mismatch at argument 1 in template parameter list for ‘template<class T> struct mixin’
     >::type> : mixin<derived> {};
                             ^
test.cc:23:29: note:   expected a type, got ‘derived’
test.cc: In function ‘int main()’:

Является ли мой единственный вариант переопределения параметров шаблона внутри предложения mixin?

1 Ответ

0 голосов
/ 04 июня 2018

Ну, попробуйте это:

#include <iostream>
#include <type_traits>

using namespace std;

template <typename T>
struct mixin {
    template <typename U>
    void operator[](U u) {
        cout << u;
    }
};


template <typename T, typename = void>
struct derived : mixin<derived<T>> {};


template <typename T>
struct derived<T,
    typename enable_if<
        is_same<T, int>::value
    >::type> : mixin<derived<T>> {};


int main() {
    derived<int> d;
    d[3.14];
}

Это делает Работа ...

Что я изменил:

  1. Использование is_same<foo,bar>::value, а не is_same<foo,bar>{} edit: Хм, кажется, вам не нужно все это менять.Neat!
  2. Не пытается заставить компилятор выводить параметр шаблона для производного при использовании mixin<derived>.Вы были способ слишком оптимистично там ...
...