我试着加入一个
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];
}
随着叮当声这给:
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子句中重新指定模板参数的唯一选项吗?