简体   繁体   English

仅在C ++标准中公开?

[英]Exposition only in the C++ standard?

What does exposition only exactly means in the C++ standard? 博览会在C ++标准中确切意味着什么? Does it mean that the private/protected members marked exposition only are required to exist by standard, or are they just a "suggestion" of implementation, and are not required at all? 这是否意味着标记为博览会的私有/受保护成员需要按标准存在,或者它们只是实施的“建议”,而根本不需要?

Examples include: 示例包括:

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

It means that they're not required by the standard, but they are just illustrating what the class's internals might look like, to give an idea of what kind of implementation the standards committee had in mind. 这意味着,他们不是标准所要求的,但他们只是说明类的内部可能是什么样子,给什么样的执行标准委员会在脑子里的想法。

It's basically a way of communicating intent. 基本上,这是一种传达意图的方式。

Exposition-only members are used to simplify a behavioural specification. 仅博览会成员用于简化行为规范。 Once the exposition-only member has been introduced and given a name, the semantics of the class can be specified in terms of it, but it is understood that what's being specified are only the semantics of the class, and the member itself is not part of that. 一旦引入了仅博览会成员并给出了名称,就可以根据该类指定类的语义,但是可以理解,所指定的只是该类的语义,该成员本身不属于该类其中。 Any conforming implementation only has to behave the same as way as described in the specification that refers to the member. 任何符合要求的实现都必须具有与引用该成员的规范中描述的方式相同的行为。

For example, suppose I want to specify a pointer wrapper class that exposes the wrappee. 例如,假设我要指定一个暴露包装的指针包装器类。 I could say, "Class Foo holds a reference to an object of type T which is given its constructor, and Foo::get exposes that object." 我可以说:“ Class Foo持有对T类型对象的引用,该对象已获得其构造函数,而Foo::get公开该对象。” That's very verbose and imprecise. 这是非常冗长和不精确的。 Alternatively, I could specify this with an exposition-only member: 另外,我可以使用仅博览会成员指定此名称:

Class Foo holds a reference to an object of type T . Foo类持有对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; } // / }; 

The specification of the individual member functions becomes much easier when I'm allowed to refer to some particular implementation, but it is understood that you can implement this in any way you like (eg with base classes or private nested types), and that the member Foo::ptr is not part of the specification. 当允许我引用某些特定的实现时,单个成员函数的规范将变得更加容易,但是可以理解的是,您可以按照自己喜欢的任何方式(例如,使用基类或私有嵌套类型)实现此功能,并且成员Foo::ptr 属于规范的一部分。 But having it allows me to specify the sematnics of the member functions in code rather than in words. 但是有了它,我可以用代码而不是文字来指定成员函数的语义。

n4296 17.5.2.3/2 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 

It indicates one of the many possible ways to implement the particular item, but not necessarily the best way. 它指示了实现特定项目的多种可能方式之一,但不一定是最佳方式。

See this answer. 看到这个答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM