Understanding copy initialization in C ++ versus explicit initialization

Why is the first commented line compiled correctly and the second not?

Why can a be provided as a constructor argument, but b cannot?
Don't these two do the same?

 class Foo { Foo &operator =(Foo const &); /* Disable assignment */ }; int main() { Foo a = a; // OK Foo b(b); // error C2065: 'b' : undeclared identifier } 

Update

Since this seems like a compiler dependent one, the problem seems to be more serious than I thought.
So I guess the other part of the question is: is the following code valid or not?

It gives an error in GCC, but Visual C ++ does it fine.

 int main() { int i = 0; { int *i(&i); } return i; } 
+8
c ++ constructor initialization
source share
2 answers

In your first code, both declarations should be compiled. GCC is right here. Visual C ++ Compiler has an error.

And in the second code, the internal declaration should not be compiled. GCC is there too, and VC ++ is wrong.

GCC is right in both cases .

Code of type int a=a+100; and int a(a+100); different from the syntactic point of view. They can refer to undefined behavior depending on whether they are created in static storage or in automatic storage mode.

 int a = a + 100; //well-defined. a is initialized to 100 //a on RHS is statically initialized to 0 //then a on LHS is dynamically initialized to (0+100). void f() { int b = b + 100; //undefined-behavior. b on RHS is uninitialized int a = a + 50; //which `a` is on the RHS? previously declared one? //No. `a` on RHS refers to the newly declared one. //the part `int a` declares a variable, which hides //any symbol with same name declared in outer scope, //then `=a+50` is the initializer part. //since a on RHS is uninitialized, it invokes UB } 

Read the comments associated with each expression above.

Note that variables with static storage duration are statically initialized to zero at compile time, and if they have an initializer, they are dynamically initialized also at runtime. But POD type variables with automatic storage duration are not statically initialized.

For more information on static initialization and dynamic initialization, see this:

  • What is dynamic object initialization in C ++?
+4
source share

In the first example, as you noticed, the behavior is undefined, although the syntax is fine. Therefore, the compiler is allowed to abandon the code (undefined behavior must be guaranteed, but it is here, but this does not mean that invalid initializations were never actually performed).

In your second example, there is an error like: an ad is displayed as soon as its declarator is viewed, and, in particular, it is displayed in its own initializer. MSVC ++ delays visibility: this is a known mismatch problem in this compiler. For example, with the EDG compiler (which has Microsoft mode):

 $ ./cfe --strict xc "xc", line 4: error: a value of type "int **" cannot be used to initialize an entity of type "int *" { int *i(&i); } ^ 1 error detected in the compilation of "xc". $ ./cfe --microsoft xc "xc", line 4: warning: variable "i" was declared but never referenced { int *i(&i); } ^ 
+1
source share

All Articles