Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to initialize a variable in C++

I have the following code :

bool c (a == b);

and

bool c {a == b};

where a and b are some variables of same type.

I want to know that, what is the difference in above two initializations and which one should be preferred in what conditions ? Any kind of help will be appreciated.

like image 969
l'-'l Avatar asked Aug 20 '15 09:08

l'-'l


People also ask

How do you initialize a variable in C?

Different ways of initializing a variable in Cint a, b; a = b = 10; int a, b = 10, c = 20; Method 5 (Dynamic Initialization : Value is being assigned to variable at run time.)

Which is correct way of variable initialization?

When you declare a variable, you should also initialize it. Two types of variable initialization exist: explicit and implicit. Variables are explicitly initialized if they are assigned a value in the declaration statement. Implicit initialization occurs when variables are assigned a value during processing.

Can we initialize variable in C?

Unlike PASCAL, in C variables may be initialized with a value when they are declared. Consider the following declaration, which declares an integer variable count which is initialized to 10. Lets examine what the default value a variable is assigned when its declared.

What is initializing in C?

Initialization is the process of locating and using the defined values for variable data that is used by a computer program. For example, an operating system or application program is installed with default or user-specified values that determine certain aspects of how the system or program is to function.


2 Answers

Both forms are direct initialization.

Using curly braces {} for initialization checks for narrowing conversions and generates an error if such a conversion happens. Unlike (). (gcc issues a warning by default and needs -Werror=narrowing compiler option to generate an error when narrowing occurs.)

Another use of curly braces {} is for uniform initialization: initialize both types with and without constructors using the same syntax, e.g.:

template<class T, class... Args>
T create(Args&&... args) {
    T value{std::forward<Args>(args)...}; // <--- uniform initialization + perfect forwarding
    return value;
}

struct X { int a, b; };
struct Y { Y(int, int, int); };

int main() {
    auto x = create<X>(1, 2);    // POD
    auto y = create<Y>(1, 2, 3); // A class with a constructor.
    auto z = create<int>(1);     // built-in type
}

The only drawback of using curly braces {} for initialization is its interaction with auto keyword. auto deduces {} as std::initializer_list, which is a known issue, see "Auto and braced-init-lists".

like image 93
Maxim Egorushkin Avatar answered Sep 21 '22 18:09

Maxim Egorushkin


First one is the C++03 style direct initialization. The second is C++11 style direct initialization, it additionally checks for narrowing conversions. Herb Sutter recommends the following in new code:

auto c = <expression>;

or when you want to commit to specific type T:

auto c = T{<expression>};

One known drawback with curly braces when T is some class with overloaded constructor, where one constructor gets std::initializer_list as parameter, std::vector for example:

auto v = std::vector<int>{10}; // create vector<int> with one element = 10
auto v = std::vector<int>(10); // create vector<int> with 10 integer elements
like image 31
Elohim Meth Avatar answered Sep 22 '22 18:09

Elohim Meth