1

I'm reading the c++ standard and I've encountered with the line (3.3.2/2):

int x=x;

In this example x is initialized with its own (Indeterminate) value. Is x initialized in this case?

1 Answers1

1

If you run this code in VS with all checks on, you'll get a runtime error

Run-Time Check Failure #3 - The variable 'x' is being used without being initialized.

The process of initialization, (roughly) assigns a value to an object. When you use the object itself, you're using uninitialized memory to initialize memory

On initialization itself, I'd recommend chapters 8.5 :

A declarator can specify an initial value for the identifier being declared. The identifier designates a variable being initialized. The process of initialization described in the remainder of 8.5 applies also to initializations specified by other syntactic contexts, such as the initialization of function parameters with argument expressions (5.2.2) or the initialization of return values. ( . . . )

and 12.6 :

12.6 Initialization [class.init]

  1. When no initializer is specified for an object of (possibly cv-qualified) class type (or array thereof), or the initializer has the form (), the object is initialized as specified in 8.5.

  2. An object of class type (or array thereof ) can be explicitly initialized; see 12.6.1 and 12.6.2.

  3. When an array of class objects is initialized (either explicitly or implicitly) and the elements are initialized by constructor, the constructor shall be called for each element of the array, following the subscript order; see 8.3.4. [ Note: Destructors for the array elements are called in reverse order of their construction. — end note ]

12.6.1 An object of class type can be initialized with a parenthesized expression-list , where the expression-list is construed as an argument list for a constructor that is called to initialize the object. Alternatively, a single assignment-expression can be specified as an initializer using the = form of initialization. Either direct-initialization semantics or copy-initialization semantics apply; see 8.5. ( . . . )

Community
  • 1
  • 1
Nikos Athanasiou
  • 29,616
  • 15
  • 87
  • 153