仅在 C++ 标准中阐述?

Exposition only in the C++ standard?

exposition only 在 C++ 标准中的确切含义是什么?这是否意味着标记为 exposition only 的 private/protected 成员必须按标准存在,或者它们只是 "suggestion" 的实现,根本不需要?

示例包括:

std::error_code::val_
std::wstring_convert::byte_err_string
std::array::elems
std::move_iterator::current
std::reverse_iterator::current
std::ostream_iterator::delim
// And a lot of others

n4296 17.5.2.3/2

Objects of certain classes are sometimes required by the external specifications of their classes to store data, apparently in member objects. For the sake of exposition, some subclauses provide representative declara- tions, and semantic requirements, for private member objects of classes that meet the external specifications of the classes. The declarations for such member objects and the definitions of related member types are followed by a comment that ends with exposition only, as in:

streambuf* sb; // exposition only

它表示实现特定项目的许多可能方法之一,但不一定是最佳方法。

参见 this 答案。

这意味着它们不是标准所要求的,但它们只是说明 class 的内部结构 可能 的样子,以提供一个想法标准委员会考虑了哪种实施方式。

这基本上是一种传达意图的方式。

Exposition-only 成员用于简化行为规范。一旦引入了 exposition-only 成员并为其命名,就可以根据它来指定 class 的语义,但据了解,指定的内容是 only class 的语义,而成员本身不是其中的一部分。任何符合规范的实现只需要与引用成员的规范中描述的方式相同。

例如,假设我想指定一个公开包装的指针包装器 class。我可以说,"Class Foo holds a reference to an object of type T which is given its constructor, and Foo::get exposes that object." 这是非常冗长和不精确的。或者,我可以用一个仅供说明的成员来指定它:

Class Foo holds a reference to an object of type T.

class Foo {
  const T* ptr;   // exposition-only

public:
  // Constructor                                 // \
  Foo(const T& t) : ptr(std::addressof(t)) {}    //  |
                                                 //   >  real specification
  // Accessor                                    //  |
  const T& get() const { return *ptr; }          // /
};

当我被允许引用某些特定的实现时,单个成员函数的规范变得容易得多,但据了解,您可以以任何您喜欢的方式实现它(例如使用 base class es 或私有嵌套类型),并且成员 Foo::ptr 是规范的 而不是 的一部分。但是有了它,我就可以用代码而不是文字来指定成员函数的语义。