C++11 example of a value and an object in the standard library?

前端 未结 3 771
栀梦
栀梦 2021-01-13 06:11

In C++11 3p3 it states:

An entity is a value, object, reference, function, enumerator, type, class member, template,

相关标签:
3条回答
  • 2021-01-13 06:51

    The standard iostream objects defined in 27.4.1 [iostreams.objects.overview] such as cin, , cout, cerr and clog (and their wide character cousins) would be examples of objects defined by the Standard Library.

    Similarly, the std::ios_base::fmtflags type defined in 27.5.3 [ios.base] has several constexpr values defined (e.g. boolalpha).

    0 讨论(0)
  • 2021-01-13 06:58

    An object is something that is stored in memory (cf. 1.8: "An object is a region of storage"). Every object has a value (which is itself), but values are more general, in the sense that the evaluation of every expression gives a value. For example, a prvalue, such as the value of f() for a declared function T f();, may or may not have storage – you cannot take its address, and its existence may not need to be manifest as storage. (However, once you bind the value to a reference variable or formal parameter, you now have a way of referring to the object by name.)

    The difference is mainly one of language semantics, though, and not usually of practical importance. (For example, people often refer to "temporary objects", although "temporary value" would be more accurate.) Values and objects both have types (which are always object types), an object can be evaluated to produce a value, and a value can be treated as an object. I would use "object" when talking about the code design, allocations and storage, and "value" when talking about grammatical rules of the language.

    0 讨论(0)
  • 2021-01-13 07:04

    The C++ standard does not provide a definition for 'value', relying on its ordinary English meaning. It defines 'object' as a 'region of storage'.

    The C++ standard library provides many values that are not objects. A simple example is NULL. Others include SIZE_MAX, EXIT_SUCCESS and FE_OVERFLOW. You may quibble over whether they are 'defined', since little explanation is provided in the standard.

    The C++ standard library provides few definitions of objects that I can find, in the sense of a name for a 'region of storage'. The only ones I know of (courtesy of a commenter) are the 'standard iostream objects' such as cin and cout.

    Since it includes the C standard library, another obvious one is errno, although even this includes a quibble that 'errno may not be the identifier of an object'.

    The standard libary does provide a large number of functions that return a pointer to an object on execution, new and malloc() being obvious examples. So it defines many dynamic objects, if you like.

    [edited to include iostream objects, new]

    0 讨论(0)
提交回复
热议问题