gpt4 book ai didi

c++ - 模板参数中的 is_base_of - 类不命名类型

转载 作者:太空宇宙 更新时间:2023-11-04 15:04:01 27 4
gpt4 key购买 nike

只是想知道为什么我不能像下面这样在 C++ 模板参数中使用 is_base_of:

#include <iostream>
#include <type_traits>
using namespace std;

struct base
{
};

struct derived : public base
{
int value;
};




template<class A, class B, bool isbaseof = std::is_base_of<A,B>::value> myclass
{
void foo() {
cout << "Generic..";
}
};

template<class A, class B> myclass<A,B,true>
{
void foo() {
cout << "A is not base of B";
}
};

int main() {

return 0;
}

错误:

prog.cpp:17:73: error: ‘myclass’ does not name a type
template<class A, class B, bool isbaseof = std::is_base_of<A,B>::value> myclass

我哪里错了?

最佳答案

您忘记了类(class) key :

   template<class A, class B, bool isbaseof = std::is_base_of<A, B>::value>
struct myclass
// ^^^^^^

(类键可以是classstructunion中的任意一个。)

关于c++ - 模板参数中的 is_base_of - 类不命名类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21514207/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com