Namespaces
Variants
Actions

Difference between revisions of "intro/variables"

From cppreference.com
(Divided Text into sections and rewrote the part about uninitialized variables)
m (Goofed on common lower bound for 32-bit signed int)
 
(8 intermediate revisions by 4 users not shown)
Line 1: Line 1:
===Variables===
+
{{title|Variables}}
  
A variable in most general definition is a named chunk of data. They directly correspond to a piece of memory somewhere on the computer. A variable holds a single value all the time, which can be modified, replaced, or used in other computations.
+
In general programming, '''variables''' are objects whose value can change – objects that can vary.
 +
This is in contrast with constants, whose value cannot change.
  
==== Creation ====
+
Variables are fundamental to programming, because without them, a program’s behaviour could not change – it would run the same way every time – and even simple programs would require enormous amounts of memory because, if objects’ values cannot change, each new value needed would need a new object.
  
In C++, all variables must be ''declared'' before use. For example:
+
== Variables in C++ ==
  
{{source|int x;}}
+
In C++, a variable is actually just a bit of memory that has been reserved for the program’s use.
 +
You refer to it using a variable name, so you don’t need to worry about where it is in memory (though you can find out its memory address, and even specify its location, if you want).
 +
The C++ type system keeps track of the size of the memory block, and how to interpret the value in it (that is, it will know whether the value {{tt|65}} in a certain memory block actually refers to the number {{math|65}}, the letter ‘{{tt|A}}’ (the ASCII/Unicode code number for the character ‘A’ is {{tt|65}}), or even perhaps a complex number with real part {{math|6}} and imaginary part {{math|5}}).
  
This is a declaration statement, which declares a variable of type {{c|int}} (integer). Such variable can hold integer values, such as {{math|-100}}, {{math|0}}, {{math|1}}, {{math|10}}, etc.  
+
To create a variable in C++, you need at least two things: a name and a type.
  
What the above statement did behind the scenes is to instruct the compiler to reserve some memory and refer all usages of the variable to that piece of memory.
+
=== Identifiers ===
  
==== Using Variables ====
+
Variable names are '''identifiers'''.
 +
Identifiers are labels used for various things in C++, including functions, classes, and namespaces as well as variables.
  
Once the variable is declared (the memory is reserved), it can be used in a variety of ways. For example, it can be assigned a value:
+
The rules for identifiers are the same whether the identifier refers to a variable, function, or whatever else.
 +
Identifiers can be pretty much whatever you like, taking into account a few rules.
 +
In general, the name of an identifier must consist of the characters {{tt|a-z}}, {{tt|A-Z}}, {{tt|0-9}} and {{tt|_}}, not begin with a digit or {{tt|_}} and not be a [[glossary#keyword|keyword]]. There are more rules that must be followed. You can read about them [[identifiers|here]].
  
{{source|1=x = 10;}}
+
Identifiers are usually not stored in the final program – unless you use certain debugging settings – so you don’t have worry about descriptive names giving hackers clues.
 +
You also don’t have to worry about the size of identifiers; if they’re stripped out, they take up no space in the final program.
  
Now the variable holds a value of {{math|10}}. This value can be used in computations, for example:
+
=== Types ===
  
{{source|1=
+
C++ is a '''strongly-typed language''', which means that when you say a variable is an integer, it stays an integer.
int y;
+
This is unlike weakly-typed languages like Javascript/ECMAScript, where variables can change types.
y = x * 5;
+
}}
+
  
A newly declared variable {{tt|y}} now holds a value of {{math|50}}. The above can be written in a shorter way, for example {{c|1=int y = x * 5;}}. This is equivalent to the above code.
+
The two reasons for this are efficiency and safety.
 +
If you allow variable’s types to change, you must keep track of what type a variable currently is, which has a cost.
 +
Keeping types fixed eliminates that cost.
 +
As for safety, consider this: in Javascript, if variable {{tt|a}} equals {{tt|1}} and variable {{tt|b}} equals {{tt|2}}, and both are integers, then “{{tt|a + b}}” gives {{tt|3}}... if both are text strings, {{tt|a + b}}” gives {{tt|12}}... but what do you get if one is an integer and one is a string?
 +
You could make up rules to answer the question, but that just makes things more complicated.
 +
It is safer to require programmers to explicitly say whether they want to add two integers or two strings.
 +
Type safety is a very important feature of C++, that makes your code efficient and easy to predict how it will behave.
  
An assignment operation  will copy the value on right hand side to the variable on the left hand side. These variables will still be independent after the operation.  
+
There are several fundamental types in C++ – the exact number depends on the version.
In the following example {{tt|y}} will retain its original value:
+
Here are some of the more basic ones:
{{source|1=
+
x = y;
+
x = 5;
+
}}
+
  
==== Using uninitialized Variables ====
+
* Signed integer types, which can hold positive and negative whole numbers. The basic signed integer type is {{c|int}}, which can hold at least {{math|−32,767}} to {{math|32,767}}.
 +
* Unsigned integer types, which can hold only positive whole numbers. The basic unsigned integer type is {{c|unsigned int}}, which can hold at least {{math|0}} to {{math|65,535}}.
 +
* Character types, which can hold characters, including letters, digits, punctuation, spaces, and control codes (like newline and tab). The basic character type is {{c|char}}, which is guaranteed to hold at least all upper and lowercase Latin letters (‘{{tt|a}}’ – ‘{{tt|z}}’ and ‘{{tt|A}}’ – ‘{{tt|Z}}’) and digits (‘{{tt|0}}’ – ‘{{tt|9}}’), some basic punctuation (‘{{tt|{}}’, ‘{{tt|}}}’, ‘{{tt|[}}’, ‘{{tt|]}}’, ‘{{tt|#}}’, ‘{{tt|(}}’, ‘{{tt|)}}’, ‘{{tt|<}}’, ‘{{tt|>}}’, ‘{{tt|%}}’, ‘{{tt|:}}’, ‘{{tt|;}}’, ‘{{tt|.}}’, ‘{{tt|?}}’, ‘{{tt|*}}’, ‘{{tt|+}}’, ‘{{tt|-}}’, ‘{{tt|/}}’, ‘{{tt|^}}’, ‘{{tt|&}}’, ‘{{tt|{{!}}}}’, ‘{{tt|∼}}’, ‘{{tt|!}}’, ‘{{tt|{{=}}}}’, ‘{{tt|,}}’, ‘{{tt|\}}’, ‘{{tt|"}}’, ‘{{tt|'}}’, ‘{{tt|_}}’), and some control codes (like carriage return, newline, tab, and null).
 +
* Floating point types, which store real numbers in two parts – a mantissa and an exponent.
 +
For example, it would rewrite {{math|1.25}} as {{math|1.25×10⁰}}, then store it as {{{math|1.25}}, {{math|0}}}; {{math|0.0043}} → {{math|4.3×10⁻³}} → {{{math|4.3}}, {{math|−3}}}; {{math|−25000}} → {{math|2.5×10⁴}} → {{{math|−2.5}}, {{math|5}}}.
 +
This enables the storage of huge or tiny numbers, but with limited precision (few significant digits). The basic floating point type is {{c|double}}. The limits of floating point numbers is a complicated topic, but double is guaranteed to hold positive values as large as at least {{math|1.79769313486231571×10³⁰⁸}} and at least as small as {{math|2.22507385850720138×10⁻³⁰⁸}}.
  
C++ strictly outlaws reading uninitialized variables as “[[undefined behaviour]]”. However, this does not imply, that compilers will prevent you from doing this:
+
Note that the limits given are just the minimum limits.
 +
On some systems, the limits can be much greater.
 +
For example, on 32-bit systems, the range of an {{c|int}} is usually {{math|−2,147,483,648}} to {{math|2,147,483,647}}.
  
{{source|1=
+
C++ also includes dozens of types in its standard library, like {{c|std::string}} to hold text strings and {{c|std::vector}} to hold multiple values.
int z;
+
y = z * 2;
+
}}
+
  
The C++-standard imposes no requirements of ''any kind'' on this program. Not only is the value of y and z completely unpredictable, but both the compiler and the created executable are allowed to erase your hard drive.
+
== Creating variables ==
  
Therefore, make sure that all variables are initialized correctly and listen carefull to compiler-warnings.
+
C++ requires you to declare all variables before you use them – it does not allow variables to appear out of nowhere.
 +
However, C++ is flexible about where you can declare them.
  
On the other hand it is perfectly legal, though questionable, to let a variable uninitialized and assign a value to it later:
+
To create a variable for use, the normal syntax is:
  
{{source|1=
+
<type> <identifier> = {<initializer>};
int z;
+
z = 3;
+
}}
+
  
==== Overview ====
+
For example, to create an integer variable named “value” with an initial value of {{tt|42}}, you would write:
  
The example below shows various ways how variables can be used:
+
{{source|int value {{=}} {42};}}
  
{{example
+
Here are some other examples of creating variables:
|code=
+
 
#include <iostream>
+
{{source|// A variable storing the mass of the Earth (5.97219x10^24 kg)
 +
double mass_of_earth {{=}} { 5.97219e24 };
 +
 
 +
// Create a text string with the URL of the site.
 +
std::string site_url {{=}} {"http://en.cppreference.com/"};
 +
 
 +
// Create a vector holding the first 5 positive odd numbers.
 +
// Note that it doesn’t all have to be on one line, which is handy
 +
// if you have a long list.
 +
std::vector<int> odd_numbers {{=}} {
 +
    1, 3, 5, 7, 9
 +
};}}
 +
 
 +
If you leave the braces empty, the variable will be default-constructed – that is, created with its default value.
 +
For fundamental types like {{c|int}} and {{c|double}}, the default value is zero.
 +
Default-constructed strings and vectors are empty (zero length strings and vectors with no items in them).
 +
 
 +
{{source|int v1 {{=}} {}; // initialized to 0
 +
double v2 {{=}} {}; // initialized to 0.0
 +
std::string v3 {{=}} {}; // initialized to a zero-length string
 +
std::vector<int> v4 {{=}} {}; // initialized to an empty vector}}
 +
 
 +
When creating a variable, the equal sign is optional:
 +
 
 +
{{source|int value{42};
 +
double v{};
 +
std::string s{};
 +
std::vector<int> odd_numbers{ 1, 3, 5, 7, 9 };}}
 +
 
 +
In some cases, the braces are optional (if you drop them, you must include the equal sign), or you can use parentheses instead, but this may subtly change the meaning in surprising ways:
 +
 
 +
{{source|int i {{=}} 42; // Same as "int i {{=}} {42};"
 +
double d {{=}} 6.28; // Same as "double d {{=}} {6.28};"
 +
string s {{=}} "Hello, world!"; // Same as "string s {{=}} {"Hello, world!"};"
 +
 
 +
// This doesn't work
 +
//std::vector<int> v {{=}} 1, 3, 5, 7, 9;
 +
 
 +
//int i1{6.28}; // causes an error, because 6.28 is not an integer
 +
int i2(6.28); // truncates 6.28 to an integer, so same as "int i2 {{=}} {6};"
 +
 
 +
std::vector<int> v1{3, 5}; // creates a vector with 2 values: {3, 5}
 +
std::vector<int> v1(3, 5); // creates a vector with 3 values: {5, 5, 5} }}
 +
 
 +
Unless you intend to specifically call a class’s constructor (which will be covered later) or to force a value to fit in a type (like cramming {{math|6.28}} into an {{c|int}}), it is better not to use parentheses.
 +
In cases where the braces are optional, whether to use them or not is usually a style choice.
 +
 
 +
== Using variables ==
 +
 
 +
Reading the value stored in a variable is as easy as simply using the variable’s name in an expression.
 +
 
 +
To change the value in a variable, you have to put the variable on the left-hand side of an assignment operator.
 +
The most basic assignment operator is just the equal sign (‘{{tt|{{=}}}}’), which assigns the result of whatever is on the right-hand side into the variable.
 +
 
 +
{{source|#include <iostream>
  
 
int main()
 
int main()
 
{
 
{
     int x = 6;
+
    // Create the variable, then read its value and print it.
     std::cout << "x: " << x << "\n";         // print the value of x
+
     int v1 {{=}} 42;
 +
     std::cout << v1 << '\n';
 +
   
 +
    // Change the variable value, then read & print it.
 +
    v1 {{=}} 69;
 +
    std::cout << v1 << '\n';
 +
   
 +
    // You can even use expressions to create a value.
 +
    double temp_in_F {{=}} 72;
 +
    double temp_in_C {{=}} (temp_in_F - 32) / 1.8;
 +
    std::cout << temp_in_F << "degrees Fahrenheit is " <<
 +
              temp_in_C << "degrees Celsius " << '\n';
 +
   
 +
    // You can even use expressions when changing a variable, too.
 +
    temp_in_F {{=}} 95.5;
 +
    temp_in_C {{=}} (temp_in_F - 32) / 1.8;
 +
    std::cout << temp_in_F << "degrees Fahrenheit is " <<
 +
              temp_in_C << "degrees Celsius " << '\n';
 +
   
 +
    return 0;
 +
} }}
 +
 
 +
== Type deduction ==
 +
 
 +
Starting with C++11, C++ is now smart enough to figure out the types for variables automatically.
 +
For example, if you use an integer value like {{tt|42}}, C++ can figure out that you want an {{c|int}}; if you use a decimal value like {{tt|6.28}}, C++ can figure you that you don’t want an {{c|int}}, you want a {{c|double}}.
 +
 
 +
To get type deduction, you can use {{c|auto}} as the variable type:
 +
 
 +
{{source|auto a {{=}} 42;  // the type of a is deduced as int
 +
auto b {{=}} 6.28; // the type of b is deduced as double}}
 +
 
 +
You can then use these variables as if they were created normally.
 +
(In other words, there is no difference between “{{c|auto a {{=}} 42;}}” and “{{c|int a {{=}} 42;}}”.)
 +
 
 +
Type deduction even works when initializing from expressions or other variables:
 +
 
 +
{{source|// This deduces the type as double,
 +
// because of the decimal point.
 +
auto height_in_cm {{=}} 170.0;
 +
 
 +
// This deduces the type as double,
 +
// because height_in_cm is double.
 +
auto height {{=}} height_in_cm;
 +
 
 +
// This deduces as double, because the
 +
// expression is {double * double}
 +
auto height_in_ft {{=}} height_in_cm * 0.032808399;
 +
 
 +
// This deduces as double, even though 10 is an int,
 +
// because {int * double} {{=}} double.
 +
auto height_in_mm {{=}} height_in_cm * 10;}}
 +
 
 +
Unless you need to know the type of a variable for some reason, type deduction frees you from having to keep track of variables for type safety.
 +
 
 +
Recently, a C++ expert has begun recommending a style called AAA – Almost Always Auto:
 +
 
 +
{{source|// Creating variables normally:
 +
auto v1 {{=}} /* initializer (maybe without braces when possible) */;
  
    int y = x * 2 + 2;
+
// Creating variables when you want a specific type:
    std::cout << "y: " << y << "\n";
+
auto v2 {{=}} /*type*/{/* initializer or empty for default */};
  
    x = y;
+
// Creating variables when you want to FORCE a specific type,
    std::cout << "x: " << x << "\n";
+
// OR when you want to explicitly use a certain constructor:
 +
auto v3 {{=}} /*type*/(/* initializer or empty for default */);}}
  
    x = x + 1;
+
Examples:
    std::cout << "x: " << x << "\n";
+
  
    int z;
+
{{source|auto meaning_of_life {{=}} 42;
    // std::cout << "z: " << z << "\n"; // disallowed
+
auto speed_of_light {{=}} 299.8e6;
 +
auto site_url {{=}} std::string{"http://en.cppreference.com/"};
 +
auto planet_masses {{=}} std::vector<double>{
 +
    3.3022e23,
 +
    4.8676e24,
 +
    5.9722e24,
 +
    6.4185e23,
 +
    1.8986e27,
 +
    5.6846e26,
 +
    8.6810e25,
 +
    1.0243e26
 +
};}}
  
    z = 3;
+
This makes creating variables in C++ simple and uniform, freeing you from having to worry about the type system most of the time, while also getting the benefits of type safety.
    std::cout << "z: " << z << "\n";
+
}
+
|p=true | output=
+
x: 6
+
y: 14
+
x: 14
+
x: 15
+
z: 3
+
}}
+

Latest revision as of 18:28, 5 November 2013


In general programming, variables are objects whose value can change – objects that can vary. This is in contrast with constants, whose value cannot change.

Variables are fundamental to programming, because without them, a program’s behaviour could not change – it would run the same way every time – and even simple programs would require enormous amounts of memory because, if objects’ values cannot change, each new value needed would need a new object.

Contents

Variables in C++

In C++, a variable is actually just a bit of memory that has been reserved for the program’s use. You refer to it using a variable name, so you don’t need to worry about where it is in memory (though you can find out its memory address, and even specify its location, if you want). The C++ type system keeps track of the size of the memory block, and how to interpret the value in it (that is, it will know whether the value 65 in a certain memory block actually refers to the number 65, the letter ‘A’ (the ASCII/Unicode code number for the character ‘A’ is 65), or even perhaps a complex number with real part 6 and imaginary part 5).

To create a variable in C++, you need at least two things: a name and a type.

Identifiers

Variable names are identifiers. Identifiers are labels used for various things in C++, including functions, classes, and namespaces as well as variables.

The rules for identifiers are the same whether the identifier refers to a variable, function, or whatever else. Identifiers can be pretty much whatever you like, taking into account a few rules. In general, the name of an identifier must consist of the characters a-z, A-Z, 0-9 and _, not begin with a digit or _ and not be a keyword. There are more rules that must be followed. You can read about them here.

Identifiers are usually not stored in the final program – unless you use certain debugging settings – so you don’t have worry about descriptive names giving hackers clues. You also don’t have to worry about the size of identifiers; if they’re stripped out, they take up no space in the final program.

Types

C++ is a strongly-typed language, which means that when you say a variable is an integer, it stays an integer. This is unlike weakly-typed languages like Javascript/ECMAScript, where variables can change types.

The two reasons for this are efficiency and safety. If you allow variable’s types to change, you must keep track of what type a variable currently is, which has a cost. Keeping types fixed eliminates that cost. As for safety, consider this: in Javascript, if variable a equals 1 and variable b equals 2, and both are integers, then “a + b” gives 3... if both are text strings, “a + b” gives 12... but what do you get if one is an integer and one is a string? You could make up rules to answer the question, but that just makes things more complicated. It is safer to require programmers to explicitly say whether they want to add two integers or two strings. Type safety is a very important feature of C++, that makes your code efficient and easy to predict how it will behave.

There are several fundamental types in C++ – the exact number depends on the version. Here are some of the more basic ones:

  • Signed integer types, which can hold positive and negative whole numbers. The basic signed integer type is int, which can hold at least −32,767 to 32,767.
  • Unsigned integer types, which can hold only positive whole numbers. The basic unsigned integer type is unsigned int, which can hold at least 0 to 65,535.
  • Character types, which can hold characters, including letters, digits, punctuation, spaces, and control codes (like newline and tab). The basic character type is char, which is guaranteed to hold at least all upper and lowercase Latin letters (‘a’ – ‘z’ and ‘A’ – ‘Z’) and digits (‘0’ – ‘9’), some basic punctuation (‘{’, ‘}’, ‘[’, ‘]’, ‘#’, ‘(’, ‘)’, ‘<’, ‘>’, ‘%’, ‘:’, ‘;’, ‘.’, ‘?’, ‘*’, ‘+’, ‘-’, ‘/’, ‘^’, ‘&’, ‘|’, ‘’, ‘!’, ‘=’, ‘,’, ‘\’, ‘"’, ‘'’, ‘_’), and some control codes (like carriage return, newline, tab, and null).
  • Floating point types, which store real numbers in two parts – a mantissa and an exponent.

For example, it would rewrite 1.25 as 1.25×10⁰, then store it as {1.25, 0}; 0.00434.3×10⁻³ → {4.3, −3}; −250002.5×10⁴ → {−2.5, 5}. This enables the storage of huge or tiny numbers, but with limited precision (few significant digits). The basic floating point type is double. The limits of floating point numbers is a complicated topic, but double is guaranteed to hold positive values as large as at least 1.79769313486231571×10³⁰⁸ and at least as small as 2.22507385850720138×10⁻³⁰⁸.

Note that the limits given are just the minimum limits. On some systems, the limits can be much greater. For example, on 32-bit systems, the range of an int is usually −2,147,483,648 to 2,147,483,647.

C++ also includes dozens of types in its standard library, like std::string to hold text strings and std::vector to hold multiple values.

Creating variables

C++ requires you to declare all variables before you use them – it does not allow variables to appear out of nowhere. However, C++ is flexible about where you can declare them.

To create a variable for use, the normal syntax is:

<type> <identifier> = {<initializer>};

For example, to create an integer variable named “value” with an initial value of 42, you would write:

int value = {42};

Here are some other examples of creating variables:

// A variable storing the mass of the Earth (5.97219x10^24 kg)
double mass_of_earth = { 5.97219e24 };
 
// Create a text string with the URL of the site.
std::string site_url = {"http://en.cppreference.com/"};
 
// Create a vector holding the first 5 positive odd numbers.
// Note that it doesn’t all have to be on one line, which is handy
// if you have a long list.
std::vector<int> odd_numbers = {
    1, 3, 5, 7, 9
};

If you leave the braces empty, the variable will be default-constructed – that is, created with its default value. For fundamental types like int and double, the default value is zero. Default-constructed strings and vectors are empty (zero length strings and vectors with no items in them).

int v1 = {}; // initialized to 0
double v2 = {}; // initialized to 0.0
std::string v3 = {}; // initialized to a zero-length string
std::vector<int> v4 = {}; // initialized to an empty vector

When creating a variable, the equal sign is optional:

int value{42};
double v{};
std::string s{};
std::vector<int> odd_numbers{ 1, 3, 5, 7, 9 };

In some cases, the braces are optional (if you drop them, you must include the equal sign), or you can use parentheses instead, but this may subtly change the meaning in surprising ways:

int i = 42; // Same as "int i = {42};"
double d = 6.28; // Same as "double d = {6.28};"
string s = "Hello, world!"; // Same as "string s = {"Hello, world!"};"
 
// This doesn't work
//std::vector<int> v = 1, 3, 5, 7, 9;
 
//int i1{6.28}; // causes an error, because 6.28 is not an integer
int i2(6.28); // truncates 6.28 to an integer, so same as "int i2 = {6};"
 
std::vector<int> v1{3, 5}; // creates a vector with 2 values: {3, 5}
std::vector<int> v1(3, 5); // creates a vector with 3 values: {5, 5, 5}

Unless you intend to specifically call a class’s constructor (which will be covered later) or to force a value to fit in a type (like cramming 6.28 into an int), it is better not to use parentheses. In cases where the braces are optional, whether to use them or not is usually a style choice.

Using variables

Reading the value stored in a variable is as easy as simply using the variable’s name in an expression.

To change the value in a variable, you have to put the variable on the left-hand side of an assignment operator. The most basic assignment operator is just the equal sign (‘=’), which assigns the result of whatever is on the right-hand side into the variable.

#include <iostream>
 
int main()
{
    // Create the variable, then read its value and print it.
    int v1 = 42;
    std::cout << v1 << '\n';
 
    // Change the variable value, then read & print it.
    v1 = 69;
    std::cout << v1 << '\n';
 
    // You can even use expressions to create a value.
    double temp_in_F = 72;
    double temp_in_C = (temp_in_F - 32) / 1.8;
    std::cout << temp_in_F << "degrees Fahrenheit is " <<
               temp_in_C << "degrees Celsius " << '\n';
 
    // You can even use expressions when changing a variable, too.
    temp_in_F = 95.5;
    temp_in_C = (temp_in_F - 32) / 1.8;
    std::cout << temp_in_F << "degrees Fahrenheit is " <<
               temp_in_C << "degrees Celsius " << '\n';
 
    return 0;
}

Type deduction

Starting with C++11, C++ is now smart enough to figure out the types for variables automatically. For example, if you use an integer value like 42, C++ can figure out that you want an int; if you use a decimal value like 6.28, C++ can figure you that you don’t want an int, you want a double.

To get type deduction, you can use auto as the variable type:

auto a = 42;   // the type of a is deduced as int
auto b = 6.28; // the type of b is deduced as double

You can then use these variables as if they were created normally. (In other words, there is no difference between “auto a = 42;” and “int a = 42;”.)

Type deduction even works when initializing from expressions or other variables:

// This deduces the type as double,
// because of the decimal point.
auto height_in_cm = 170.0;
 
// This deduces the type as double,
// because height_in_cm is double.
auto height = height_in_cm;
 
// This deduces as double, because the
// expression is {double * double}
auto height_in_ft = height_in_cm * 0.032808399;
 
// This deduces as double, even though 10 is an int,
// because {int * double} = double.
auto height_in_mm = height_in_cm * 10;

Unless you need to know the type of a variable for some reason, type deduction frees you from having to keep track of variables for type safety.

Recently, a C++ expert has begun recommending a style called AAA – Almost Always Auto:

// Creating variables normally:
auto v1 = /* initializer (maybe without braces when possible) */;
 
// Creating variables when you want a specific type:
auto v2 = /*type*/{/* initializer or empty for default */};
 
// Creating variables when you want to FORCE a specific type,
// OR when you want to explicitly use a certain constructor:
auto v3 = /*type*/(/* initializer or empty for default */);

Examples:

auto meaning_of_life = 42;
auto speed_of_light = 299.8e6;
auto site_url = std::string{"http://en.cppreference.com/"};
auto planet_masses = std::vector<double>{
    3.3022e23,
    4.8676e24,
    5.9722e24,
    6.4185e23,
    1.8986e27,
    5.6846e26,
    8.6810e25,
    1.0243e26
};

This makes creating variables in C++ simple and uniform, freeing you from having to worry about the type system most of the time, while also getting the benefits of type safety.