在完全专门化中,为什么继承使前向声明(没有定义)不再足够?
In a full specialization, why does inheritance make a forward declaration (without definition) no longer sufficient?
为什么在下面的代码中template<typename T> struct Child : public Parent
需要定义,而template<typename T> struct Orphan
不需要定义(但是存在一个没有伤害)?
#include <iostream>
struct Parent {};
// A definition is necessary.
template<typename T>
struct Child : public Parent
{};
template<>
struct Child<int> : public Parent
{
Child() {
std::cout << "Child<int>::Child: full specializationn";
}
};
// No definition is necessary (but the presence of one doesn't hurt).
template<typename T>
struct Orphan;
template<>
struct Orphan<int>
{
Orphan() {
std::cout << "Orphan<int>::Orphan: full specializationn";
}
};
int main()
{
Orphan<int> orphan;
Child<int> child;
}
template<typename T>
struct Orphan;
这是一个完全正常的前向声明。
我想语言只是禁止包含父类声明的前向声明。这样的语法不存在。
所以它和模板没有太大关系。
class Base {};
class Derived: public Base; //forward declaration of Derived
在您的情况下,您可以像往常一样转发声明Child
template <class T>
class Child;
现在唯一的问题是:当我们试图创建类的实例时,我们是否仍然只有一个前向声明,还是我们有一个完整的类型。
为了继承你的总是需要一个完整的类定义。
为了特化,编译器只需要知道模板类的名称。在这种情况下,当需要完整的定义时,任何专门化的使用都可以工作。任何非int
专门化版本的使用都将被视为前向声明可用。
相关文章:
- 在提升multi_index容器中,是否定义了"default index"?
- #定义c-预处理器常量..我做错了什么
- 用C++中的一个变量定义一个常量
- 部分定义/别名模板模板参数
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- #为""定义宏;静态";针对不同的上下文
- 如何确保C++函数在定义之前声明(如override关键字)
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 当类在C++中定义时,有什么方法可以"register"类吗?
- 在命名空间中定义函数还是限定函数
- 此代码是否违反一个定义规则
- 在 C++20 中是否不再允许在 std 中对程序定义类型的函数模板进行专用化?
- 当程序从该函数调谐器时,指向在函数中声明和定义的某些 C 字符串的指针不再有效.为什么?
- 检查指向自定义类的指针是否不再存在
- 如果左值不再在其定义范围内使用,编译器是否允许将左值转换为右值引用
- 为什么当定义析构函数时,编译器不再添加默认的move函数和赋值函数
- 不匹配的删除不再是未定义的行为
- 已经或将要使用C++14或C++1z使其不再未定义调用委托类成员函数指针
- 在完全专门化中,为什么继承使前向声明(没有定义)不再足够?
- 根据定义,将"虚拟析构函数放入接口内"不会使它不再是接口吗?