4

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

The added part of n3242 when compare to ISO Standard 2003 :

4 Exactly one definition of a class is required in a translation unit if the class is used in a way that requires theclass type to be complete.

A class type T must be complete if:

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

Can anyone explain this paragraph of the current C++0x standard draft?

What is the actual meaning of adding this in these statement?

Can any one explain this with the help of an example/program?

Community
  • 1
  • 1
1User
  • 614
  • 3
  • 13
  • 9
    What exactly do you find confusing about this paragraph? This is your third question in a short period of time about the ODR paragraphs. Do you have a broader question or is there some overarching issue that confuses you? This particular paragraph is quite straightforward: it lists the scenarios under which a complete type is required. Do you not know what a complete type is? Do you not understand one of those scenarios? – James McNellis Apr 11 '11 at 13:38
  • @James: Iam trying to check each and every point ...what they said ... in c++0x .And iam some what not clear ..so iam seeking help from programmer like u.... – 1User Apr 11 '11 at 13:43
  • 2
    Right, but what is your question? What part of this paragraph is confusing? If you find the quoted text confusing, you might start with [a good introductory or reference C++ text](http://stackoverflow.com/questions/388242/the-definitive-c++-book-guide-and-list) instead of with the draft standard. The C++ Standard is quite dense. Understanding one paragraph often requires understanding 30 others as well and the interrelationships between all those related paragraphs are not always readily apparent. – James McNellis Apr 11 '11 at 13:45
  • @James:how these above points proves true ... Yes what u said is right .That is what iam trying to understand each and every point with proving in terms of various programming examples .So if iam unable to prove that iam seeking for help and from help iam trying to understand those ..by again proving ... – 1User Apr 11 '11 at 13:47
  • 2
    I strongly advise against that plan: get a good introductory book to learn the basics and get a reference book or two to learn the language more in-depth. Only after you have a good foundation in the language should you start researching things in the C++ Standard. It is not an easy text to read. Since you cannot clarify what you find confusing, no one can answer this question beyond "read the rest of the Standard to understand this paragraph in context." Thus, this question does not meet the criteria for a real question on Stack Overflow. – James McNellis Apr 11 '11 at 13:57
  • The core part of the C++ standard is written by compiler implementors, for compiler implementors. You have a hard problem understanding it, because you've got no practical experience. From personal experience I'd advise you to implement a very simple language yourself. Perhaps the easiest example is a calculator (e.g. the example given in Stroustrups TC++PL). The next step would be to design & implement a toy language. – MSalters Apr 11 '11 at 15:24

2 Answers2

6

Straight from Wikipedia:

In general, a translation unit shall contain no more than one definition of any class type. In this example, two definitions of the class type C occur in the same translation unit. This typically occurs if a header file is included twice by the same source file without appropriate header guards.

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

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

Defining an object of type S, a function taking an argument of type S, or using S in a sizeof expression are examples of contexts where S must be complete, 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 certain cases, there can be more than one definition of a type or a template. A program consisting of multiple header files and source files will typically have more than one definition of a type, but not more than one definition per translation unit.

If a program contains more than one definition of a type, then each definition must be equivalent.

Definitions of static const data members

In pre-standard C++, all static data members required a definition outside of their class. However, during the C++ standardization process it was decided to lift this requirement for static const integral members. The intent was to allow uses such as:

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

without a namespace scope definition for N.

Nevertheless, the wording of the 1998 C++ standard still required a definition if the member was used in the program. This included the member appearing anywhere except as the operand to sizeof or typeid, effectively making the above ill-formed.

This was identified as a defect, and the wording was adjusted to allow such a member to appear anywhere a constant expression is required, without requiring an out-of-class definition. This includes array bounds, 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 member anywhere except where an integral constant-expression is required 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 referred to as C++0x.

  • @0AOD : WOW ,Thank q for ur EDITING . I already seen this . iam asking for exactly what is meant by Above point – 1User Apr 11 '11 at 13:51
  • @1User: If you could clarify what exactly you do not understand, I may be able to help –  Apr 11 '11 at 13:54
0

All it's saying is that if you make use of a class type (and they're explicitly listing ways to use a class) in a way that requires a definition, you must provide exactly one definition of such class.

If you don't provide a definition it's an error. If you provide more than one in a translation unit it's an error. If you provide more than one definition across multiple translation units it's undefined behavior.

Mark B
  • 95,107
  • 10
  • 109
  • 188