工会成员中的模板 - 声明没有声明任何内容
Template in union member - Declaration does not declare anything
我需要创建一个可以存储其内容(类型T
(或nullptr
(表示空节点(的"节点"类。
此节点在存储某些东西而什么都没有时必须具有元数据(就像其年龄一样(。
我提出了这个(简化的(代码:
template <typename T>
struct Node
{
union T_or_null {
T;
std::nullptr_t;
};
int age;
T_or_null content;
Node(T_or_null argContent)
: age(0),
content(argContent)
{
}
};
int main()
{
Node<int> a(0);
Node<int> b(nullptr);
return 0;
}
我正在获取错误main.cpp:5:3: error: declaration does not declare anything [-fpermissive]
GCC似乎了解我正在尝试创建任何事物和nullptr_t
的结合(这是任何事物的一部分(,但是在这种情况下,它应该是int
和nullptr_t
的结合,仅此而已。
我是否误解了模板的工作方式,还是需要做不同的事情?
工会成员也需要一个名称:
union T_or_null {
T value;
std::nullptr_t null;
};
,但它们还需要手动簿记才能正确处理,所以我建议您放弃联盟并切换到std::optional
以建模无效:
template <typename T>
struct Node
{
int age;
std::optional<T> content;
Node(std::optional<T> argContent)
: age(0),
content(argContent)
{
}
};
相关文章:
- C++ 17 个友元函数声明和内联命名空间
- GLUT 问题:重新声明 c++ 内置类型'wchar_t'时出错
- 在 C++ 中声明类内的全局成员
- C++ 如何在外部类中声明的内部类中使用变量
- 是否有任何内置的哈希图函数来处理 c++ 中的值输入?
- 是否有任何内置函数可以检查给定的两个数字在给定整数数组中的顺序是否相同?
- 将静态全局变量声明为内联有什么意义吗?
- 重新声明C++内置类型"wchar_t"Windows 10 过剩 CPP
- C++ 中是否有任何内置阶乘函数?
- 工会成员中的模板 - 声明没有声明任何内容
- 是否有任何内置函数可以告诉编译器分支是否可预测
- 海湾合作委员会警告"没有声明任何东西
- 类 'MainWindow' 中未声明任何成员函数
- 当我们不能声明函数内联(GCC 编译器)时?
- 外部模板:声明不声明任何内容
- 如何在声明时间内启动成员对象
- 我需要在C /C#中使用图形.我可以使用任何内置的C /C#库
- 是否有任何内置类可以处理C 中的大整数值
- 编译时类中未声明任何成员函数时出错
- 定义或重新声明函数内不允许'GetInstance'