为什么的std :: sub_match< T>从性病::对&LT公开继承; T,T&GT ;?

为什么的std :: sub_match< T>从性病::对&LT公开继承; T,T&GT ;?

问题描述:

我读的std :: sub_match&LT的文件; BidirectionalIterator> ,看到它公开从性病继承::对< BidirectionalIterator,BidirectionalIterator&GT ; 。由于 sub_match 简直就是一对迭代到的字符序列,与一些额外的功能,我可以理解,它与对实施,但为什么使用公共继承?

I was reading the documentation of std::sub_match<BidirectionalIterator> and saw that it publicly inherits from std::pair<BidirectionalIterator, BidirectionalIterator>. Since a sub_match is simply a pair of iterators into a sequence of characters, with some additional functions, I can understand that it is implemented with a pair, but why use public inheritance?

STD公开继承::对&LT的问题; T,U&GT; 是一样的与其他大多数标准类公开继承:他们的目的不是被操纵多态(特别是他们没有定义虚析构函数)。其他成员也将无法正常工作,即赋值运算符和交换成员函数(它们不会复制匹配成员 sub_match )。

The problem with inheriting publicly from std::pair<T,U> is the same as inheriting publicly from most other standard classes: they are not meant to be manipulated polymorphically (notably they do not define a virtual destructor). Other members will also fail to work properly, namely the assignment operator and the swap member function (they will not copy the matched member of sub_match).

为什么boost开发,然后委员会决定从对公开继承,而不是使用成分落实 sub_match (或使用的声明,如果他们想继续保持成员访问通过第一私有继承第二)?

Why did Boost developers and then the committee decided to implement sub_match by inheriting publicly from pair instead of using composition (or private inheritance with using declarations if they wanted to keep member access through first and second)?

这是一个有趣的问题。 presumably,他们认为这是安全的
因为从来没有人会动态地分配一个反正。有关
只有你会得到 sub_match 对象的方式是作为返回值
从一些 basic_regex ​​的功能,或者作为其他副本
sub_match ,而所有这些将是要么临时或局部
变量。

It's an interesting question. Presumably, they considered it safe because no one would ever dynamically allocate one anyway. About the only way you're going to get sub_match objects is as a return value from some of the functions of basic_regex, or as copies of other sub_match, and all of these will be either temporaries or local variables.

请注意,这不是安全,保持 sub_match 物体周围无论如何,因为
它们包含的迭代器一辈子......似乎并不在被指定
标准。直到的match_results 对象重用?直到
字符串操作数填补了该函数的的match_results
对象被破坏?还是?

Note that it's not safe to keep sub_match objects around anyway, since they contain iterators whose lifetime... doesn't seem to be specified in the standard. Until the match_results object is reused? Until the string operand to the function which filled in the match_results object is destructed? Or?

我仍然避免了公众的传承。但在这种情况下,它的
不是很危险,因为它看起来,因为实在没有理由你
以往要动态分配一个 sub_match

I'd still have avoided the public inheritence. But in this case, it's not as dangerous as it looks, because there's really no reason you'd ever want to dynamically allocate a sub_match.