是否允许实现将公共成员添加到标准类型? [英] Are implementations allowed to add public members to standard types?

查看:115
本文介绍了是否允许实现将公共成员添加到标准类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否允许C ++标准库实现向标准类型的接口添加public(和protected)成员? N3797 17.6.5.5 [member.functions] / 2说:


一个实现可以在类中声明额外的非虚函数成员函数签名:



- 通过向成员函数签名中添加具有默认值的参数; [注意:实施可能不会向虚拟,全局或非成员函数添加具有默认值的参数。 - end note ]



- 通过两个或更多成员函数签名替换具有默认值的成员函数签名,






p>这是否意味着在任何情况下(包括保留的标识符),标准库不能添加任何标准中没有提及的公共成员?



一点点解释:这是关于添加签名的文本(我假设谈论新的签名只是为已经定义的功能,因此没有新的名称)我设法找到标准。还有脚注189,它说:


一个有效的C ++程序总是调用预期的库成员函数或具有相同行为的库成员函数。一个实现也可以定义另外的成员函数,否则不能被有效的C ++程序调用。


.functions],所以它显然只是关于成员函数。我的问题是更通用,并要求任何参考我可能错过了:是允许添加新的名称到公共(和/或受保护的)标准类型的接口的标准库实现,无论是数据

我相信你有你需要的脚注组合 189 / code>,它说:


一个有效的C ++程序总是调用预期的库成员函数,一个实现还可以定义另外的成员函数,否则不能被有效的C ++程序调用。


17.6.5.11 的派生类:



,但不添加任何限制,即它不让我们说限制访问限定符等...



,我们可以看到 libstdc ++ 相当有效地使用派生类,例如在 stl_vector.h 。虽然我可以看到 libstdc ++ 似乎避免了添加公共数据成员,但这可能更多的是干净的设计。



至少,这看起来低于指定,但如果你坚持类似于 libstdc ++ 实现风格,你应该是好的。


Are C++ standard library implementations allowed to add public (and protected) members to standard types' interfaces? N3797 17.6.5.5 [member.functions]/2 says:

An implementation may declare additional non-virtual member function signatures within a class:

— by adding arguments with default values to a member function signature; [ Note: An implementation may not add arguments with default values to virtual, global, or non-member functions. — end note ]

— by replacing a member function signature with default values by two or more member function signatures with equivalent behavior; and

— by adding a member function signature for a member function name.

Does this mean that a standard library cannot add any additional public members with names not mentioned in the standard under any circumstances (that include, for example, reserved identifiers)?

A tiny bit of explanation: this is the text about adding signatures (which I assume talks about new signatures just for functions that are already defined to be there, so no new names) I managed to find in the standard. There is also the footnote 189, which says:

A valid C++ program always calls the expected library member function, or one with equivalent behavior. An implementation may also define additional member functions that would otherwise not be called by a valid C++ program.

All this text originates from [member.functions], so it is clearly about member functions only. My question is more generic and asks for any references I could've missed: is a standard library implementation allowed to add new names to public (and/or protected) interfaces of a standard type, be it data or function members?

解决方案

I believe you have what you need with a combination of foot note 189 which says:

A valid C++ program always calls the expected library member function, or one with equivalent behavior. An implementation may also define additional member functions that would otherwise not be called by a valid C++ program.

and section 17.6.5.11 Derived classes which says:

An implementation may derive any class in the C++ standard library from a class with a name reserved to the implementation.

but does not add any restrictions, i.e. it does not let's say restrict the access qualifiers etc...

and we can see libstdc++ uses derived classes pretty effectively, for example in stl_vector.h. Although as far as I can see libstdc++ does seem to eschew adding public data members but that is probably more for clean design.

At minimum, this looks under-specified but if you stick to something similar to libstdc++ implementation style you should be good.

这篇关于是否允许实现将公共成员添加到标准类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆