Differences between int initialization in C ++ with double

what are the differences between this type of initialization in C ++;

int a = 0;
int a{};
int ();

why should this code int a{3.14}get me an error, but this int a = 3.14or this one is int a(3.14)not

+4
source share
4 answers

int a = 0; and int a (0); have no meaning in the code generated by the machine. They are the same.

Below is the build code generated in Visual Studio

int a = 10;   // mov dword ptr [a],0Ah  
int b(10);    // mov dword ptr [b],0Ah

but int a{}slightly different due to narrowing conversions that prohibit list initialization

This is from the C ++ help site :

Conversion narrowing

an initialization list restricts valid implicit conversions by prohibiting the following:

conversion from a floating-point type to an integer type 

conversion from a long double to double or to float and conversion from double to float, except where the source is a constant expression

conversion from an integer type to a floating-point type, except where the source is a constant expression whose value can be stored

conversion from integer or unscoped enumeration type to integer type that cannot represent all values of the original, except where

source - ,

,

+2

(++ 11):

int foo = 0; // Initialize foo with 0
int foo{}; // Initialize foo with foo type (int) default value, which is 0
int foo(); // Function declaration

int bar = 5.f; // Initialize bar with 5 (narrowing conversion from floating point)
int bar{5.f}; // Doesn't compile, because there is a loss of data when casting a float to an int
int bar(5.f); // Initialize bar with 5 (narrowing conversion from floating point)

:

float f{5}; // Okay, because there is no loss of data when casting an int to a float
+4

a{3.14} , type,

int a(3.14) // initial value: 3.14, you could use {} also intead of () , , .

, , :

// Bog-standard declaration.


    int a;


// WRONG - this declares a function.

    int a();

// Bog-standard declaration, with constructor arguments.
// (*)

    int a(1, 2, 3... args);

// Bog-standard declaration, with *one* constructor argument
// (and only if there a matching, _non-explicit_ constructor).
// (**)

    int a = 1;

// Uses aggregate initialisation, inherited from C.
// Not always possible; depends on layout of T.

    int a = {1, 2, 3, 4, 5, 6};

// Invoking C++0x initializer-list constructor.

    int a{1, 2, 3, 4, 5, 6};

// This is actually two things.
// First you create a [nameless] rvalue with three
// constructor arguments (*), then you copy-construct
// a [named] T from it (**).

    int a = T(1, 2, 3);

// Heap allocation, the result of which gets stored
// in a pointer.

    int* a = new T(1, 2, 3);

// Heap allocation without constructor arguments.

    int* a = new T;
+1

int i = 42;
int j(42);

As for forced initialization, this is a C ++ function that appeared in C ++ 11 Standard. Because of this, it does not have to be compatible with C Standard, and therefore it comes with more stringent type security guarantees. Namely, this prohibits the implicit narrowing of the transformation.

int i{ 42 };
int j{ 3.14 }; // fails to compile
int k{ static_cast<int>(2.71) }; // fine

Hope this helps. Let me know if you need more information.

+1
source

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


All Articles