Can someone explain this paragraph of an existing standard C ++ 0x draft?

Can anyone explain this statement from ISO N3242 ยง3.2, 4th point

Added n3242 part when compared with ISO Standard 2003:

4 Exactly one class definition is required in a translation unit if the class is used so that the class of the class is completed.

A class type T must be complete if:

  • a non-static data member of type T (9.2) is declared or
  • T used as the type of an object or the type of an array element in a new expression
  • type T is the subject of an alignment expression (5.3.6) or
  • the exception declaration is of type T , a reference to T or a pointer to T

Can someone explain this paragraph of an existing standard C ++ 0x draft?

What is the actual meaning of adding this to this statement?

Can someone explain this with an example / program?

+4
source share
2 answers

Straight from Wikipedia :

In general, a translation unit must contain no more than one definition of any type of class. In this example, two class definitions of type C occur in a single translation block. This usually happens if the header file is included twice in the same source file without the appropriate header protection.

 class C {}; // first definition of C class C {}; // error, second definition of C 

n the following, forming a pointer to S or definitions of a function that takes a reference to S, are examples of legal constructions because they do not require that type S be complete. Therefore, a definition is not required.

Defining an object of type S, a function that takes an argument of type S, or using S in a sizeof expression, examples of contexts where S should be and therefore require a definition.

 struct S; // declaration of S S * p; // ok, no definition required void f(S&); // ok, no definition required void f(S); // ok, no definition required S f(); // ok, no definition required S s; // error, definition required sizeof(S); // error, definition required 

More than one definition

In some cases, there may be more than one type definition or pattern. A program consisting of several header files and source files usually have more than one type definition, but no more than one definition per translation block.

If the program contains several type definitions, then each definition should be equivalent.

Static Constant Data Member Definitions

In pre-standard C ++, all static data members need to define outside their class. However, during the C ++ Standardization Process, it was decided to remove this requirement for static constant integral elements. the purpose was to allow use, for example:

 struct C { static const int N = 10; }; char data[C::N]; // N "used" without out-of-class definition 

without defining a namespace scope for N.

However, the wording of the 1998 C ++ standard still required if a member was used in the program. This included a member that appears anywhere except the sizeof or typeid operand is effectively badly formed.

This was defined as a defect, and the wording was adjusted so that such a term should appear anywhere a constant expression is required without requiring an extra-curricular definition. This includes an array of borders, case expressions, static member initializers, and nontype template arguments.

 struct C { static const int N = 10; static const int U = N; // Legal per C++03 }; char data[C::N]; // Legal per C++03 template<int> struct D; template<> struct D<C::N> {}; // Legal per C++03 

However, using a static const-integral term anywhere other than an integral constant expression requires a definition

 struct C { static const int N = 10; }; int main() { int i = C::N; // ill-formed, definition of C::N required } 

This requirement will be relaxed in the upcoming C ++ standard, colloquially called C ++ 0x.

+6
source

All he says is that if you use a class type (and they explicitly enumerate how the class is used) in a way that requires a definition, you must provide exactly one definition of that class.

If you did not specify a definition, this is a mistake. If you provide multiple translation units, this is a mistake. If you provide multiple definitions for multiple translation units, this behavior is undefined.

0
source

Source: https://habr.com/ru/post/1347546/


All Articles