gpt4 book ai didi

c++ - 是否允许实现将公共(public)成员添加到标准类型?

转载 作者:IT老高 更新时间:2023-10-28 22:41:00 25 4
gpt4 key购买 nike

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

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.

这是否意味着标准库在任何情况下都不能添加任何其他名称未在标准中提及的公共(public)成员(包括,例如,保留标识符)?

一点解释:这是关于添加签名的文本(我假设讨论新签名只是针对已经定义在那里的函数,所以没有新的名称)我设法在标准中找到。还有脚注189,上面写着:

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.

所有这些文本都来自 [member.functions],因此很明显它只是关于成员函数。我的问题更通用,并询问我可能错过的任何引用:标准库实现是否允许向标准类型的公共(public)(和/或 protected )接口(interface)添加新的 names,无论是数据还是函数成员?

最佳答案

我相信你有你需要的脚注组合 189 说:

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.

和部分 17.6.5.11 派生类 说:

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

但不添加任何限制,即它不会限制访问限定符等...

我们可以看到libstdc++非常有效地使用派生类,例如在 stl_vector.h 中.尽管据我所知 libstdc++ 似乎确实避免添加公共(public)数据成员,但这可能更适合简洁的设计。

至少,这看起来没有指定,但如果你坚持使用类似于 libstdc++ 实现风格的东西,你应该会很好。

关于c++ - 是否允许实现将公共(public)成员添加到标准类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21539291/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com