Are implementations allowed to add public members to standard types?

放肆的年华 提交于 2019-12-03 11:42:31

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.

I think that the key to reading footnote 189 is the phrase would otherwise not be called by a valid C++ program.

Remember that identifiers beginning with an underscore followed by a capital letter (or containing two consecutive underscores anywhere) are reserved for the implementation. (section 17.6.4.3.2)

So implementations are free to add public/protected member functions that are named in that manner.

For example, in libc++, std::vector has a protected member function named __throw_length_error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!