C++ c++;模板-类型/值不匹配-使用依赖于派生类的类型实例化基类模板

C++ c++;模板-类型/值不匹配-使用依赖于派生类的类型实例化基类模板,c++,templates,inheritance,C++,Templates,Inheritance,以下代码未能编译,出现类型/值不匹配错误,但我知道提供了一个类型。我错过了什么 template<typename A, typename B> struct base {}; template<typename B> struct derived : base< derived<B>::type, B > { using type = int; } int main() { derived<char> d; } erro

以下代码未能编译,出现类型/值不匹配错误,但我知道提供了一个类型。我错过了什么

template<typename A, typename B>
struct base {};

template<typename B>
struct derived : base< derived<B>::type, B >
{
  using type = int;
}

int main()
{
  derived<char> d;
}

error: type/value mismatch at argument 1 in template parameter
list for 'template<class A, class B> struct base'
struct derived : base< derived<B>::type, B >

note: expected a type, got 'derived<B>::type'
模板
结构基{};
模板
结构派生:基<派生::类型,B>
{
使用type=int;
}
int main()
{
导出d;
}
错误:模板参数中参数1的类型/值不匹配
“模板结构基础”的列表
结构派生:基<派生::类型,B>
注意:应为类型,但得到“派生::类型”
为什么
derived::type
不是有效的类型

此外,他还尝试了以下做法:

template<typename B>
struct derived : base< typename derived<B>::type, B >
{
  using type = int;
}
模板
结构派生:基
{
使用type=int;
}
并得到以下错误:

no type name 'type' in 'struct derived<char>'
在“struct-derived”中没有类型名“type”
为什么编译器无法检测类型

为什么编译器无法检测类型?

类被认为是完全定义的对象类型([basic.types]) (或完整类型)在类说明符的结尾处

因此,
struct-derived
的类型不完整,将
derived::type
声明为模板参数将是格式错误的

为什么编译器无法检测类型?

类被认为是完全定义的对象类型([basic.types]) (或完整类型)在类说明符的结尾处


因此,
struct-derived
的类型不完整,将
derived::type
声明为模板参数将是格式错误的。

您在这里看到两个不同的问题。第一个是
derived::type
是一个依赖类型名,因此必须使用
typename
关键字通知编译器它是一个类型而不是一个对象:

template<typename B>
struct derived : base< typename derived<B>::type, B >
{
  using type = int;
}

您在这里看到了两个不同的问题。第一个是
derived::type
是一个依赖类型名,因此必须使用
typename
关键字通知编译器它是一个类型而不是一个对象:

template<typename B>
struct derived : base< typename derived<B>::type, B >
{
  using type = int;
}

template <typename B>
struct my_trait
{
    using type = int;
};

template <typename B>
struct derived : base < typename my_trait<B>::type, B >
{
    using type = typename my_trait<B>::type;
};