我正在试验并写了这个怪物:
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 thetypedef
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/