c++ - 这种元组的递归引用安全吗?

标签 c++ c++11 visual-studio-2013

我正在试验并写了这个怪物:

class my_tuple : public std::tuple < std::vector<my_tuple> > {};

看起来它可以编译并且实际上可以工作。我发现它很狡猾,因为以下内容无法编译:

using my_other_tuple = std::tuple < std::vector<my_other_tuple> > ;

最终,我试图弄清楚 my_tuple 为何有效以及是否存在任何潜在的可怕后果。我正在尝试了解元组的全部内容以及我可以/应该用它们做什么。因此,如果有人愿意对此发表评论,提供一些精彩的见解,我将不胜感激。

Windows 7 和 VS 2013。

最佳答案

class my_tuple : public std::tuple < std::vector<my_tuple> > {};

这是当前未定义的行为,因为它实例化了一个标准库容器 std::vector,其类型不完整,my_tuple,直到关闭才变得完整} 其定义。然而,有一个proposal允许实例化某些标准容器,包括具有不完整类型的 std::vector 。 Boost.Containers supports incomplete types as well.

using my_other_tuple = std::tuple < std::vector<my_other_tuple> > ;

这是格式不正确的。 [dcl.typedef]/p2,强调我的:

A typedef-name can also be introduced by an alias-declaration. The identifier following the using keyword becomes a typedef-name and the optional attribute-specifier-seq following the identifier appertains to that typedef-name. It has the same semantics as if it were introduced by the typedef specifier. In particular, it does not define a new type and it shall not appear in the type-id.

关于c++ - 这种元组的递归引用安全吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28247170/

相关文章:

c++ - 有符号零的最小值和最大值

c++ - Constexpr 变量在 Lambda 内部显示为未初始化

STL vector+sort+equality vs. unordered_set vs. using pure set 的性能(内存和速度方面)

c# - 项目之间的依赖注入(inject)

c++ - 在理解 C++ 基本类的一小部分方面需要帮助吗?

c++ - 使用 OpenSSL 的相互认证总是成功的

C++11 "late binding"模板参数

node.js - 在 Visual Studio 2013 上运行波纹模拟器时出现问题

.net - 在 Windows 10 上安装 .Net 4.5(缺少 4.5 和 4.6 版本)

java - 当无法通过 JNI 加载 JVM 时如何获得错误消息?