Recently, while catching up on C++, we learned some new features of the C++11 standard. With these new features, we can improve our programming efficiency faster and achieve our goals. Here are some of the things we learned during the learning process for future review and review.

The auto keyword

In our daily programming, we often need to assign the value of the expression to a variable, and when declaring a variable, we must clearly know what type the variable belongs to. Sometimes, however, this is not easy. In order to solve this problem, the new C++11 standard introduced the auto type specifier. By using the auto keyword, we can have the compiler analyze the type of an expression for us, as opposed to those that only correspond to a specific type (such as int). Auto allows the compiler to derive the type that defines a variable from the initial value, thus greatly reducing the probability of variable type errors in our programming.

One thing to note ⚠️ : Auto defines variables that must have initial values.

Here’s an example:

#include <iostream>
#include <typeinfo>
using namespace std;

int main(int argc, const char *argv[])
{
  auto value1 = 1;
  auto value2 = 2.33;
  auto value3 = 'a';
  std::cout << "Value1 is of type" << typeid(value1).name() << std::endl;
  std::cout << "Value2 is of type" << typeid(value2).name() << std::endl;
  std::cout << "Value3 is of type" << typeid(value3).name() << std::endl;
  return 0;
}
Copy the code

The running results are as follows:

Value1 is of type I. Value2 is of type D. Value3 is of type CCopy the code

Note: The TypeID () operator can print the type of a variable, and its library functions are in a header file, as shown in the example 👆 above.

Sometimes the type of auto inferred by the compiler is not exactly the same as the type of the initial value. The compiler may change the result type to make it more consistent with the initialization rules. For example, we use float and double, which the compiler seems to prefer.

Note that auto can be used to declare multiple variables in a statement. However, a statement can only have one basic data type when declaring multiple variables. Therefore, the initial basic data types of all variables in the statement must be the same. Be sure to distinguish between data types and type modifiers here!!

Such as:

We add value4 and value5 to the code above:

auto value4 = "QAQ", value5 = &value1;
Copy the code

Value4 can infer the type of a string, while Value5 can infer the type of a pointer. A statement can only have one basic data type when declaring multiple variables.

The decltype keyword

The decltype type specifier is also standard in C++11. It is used to infer the type of the variable to be defined from the type of the expression.

Because sometimes, we have situations like this:

The second type specifier, decltype, is introduced in C++11 when we want to infer the type of a variable from an expression but do not want to initialize the variable with the value of the expression, or when the return type of the function is the type of the value of an expression. It selects and returns the data type of the operand. In this process, the compiler simply analyzes the type of the return value of an expression or function and obtains its type, without actually evaluating the value of the expression.

Here’s an example:

#include <iostream>
#include <typeinfo>
using namespace std;

std::string func(a){
  return "Hello World!";
}

int main(int argc, const char *argv[])
{
  decltype(func()) a;
  int i;
  decltype(i) b;
  std::cout << "Is the type of A?" << typeid(a).name() << std::endl;
  std::cout << "The type of B is << typeid(b).name() << std::endl;
  return 0;
}
Copy the code

The following output is displayed:

The type of A is NSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE. The type of B is ICopy the code

Decltype ((variable)) is always a reference, whereas decltype((variable)) is a reference only if variable is itself a reference.

The difference between the auto keyword and decltype keyword

For expressions used in decltype, if a pair of parentheses is added to the variable name, the resulting type may be different from that without parentheses.

If declType uses an unparenthesized variable, the result is the type of the variable. The compiler treats the variable as an expression. The variable is a special expression that can be used as an lvalue. Therefore, such declType returns the reference type.

Such as:

int i;
decltype(i) / / int type
decltype((i)) / / int & type
Copy the code

We will not discuss too many specific details here, but just introduce some common usage and precautions, for more information refer to C++ Primer Plus.