gogoWebsite

Common types of C++ (auto, type conversion, decltype)

Updated to 2 days ago

1 Type conversion

1.1 Convert string to int, double, float and other types

Based on the c++11 standard, the corresponding conversion method has been encapsulated in <string>

#include<string>
 using namespace std;//Add namespace

 string d="123";
 double num=stod(d);//string convert double
 int num=stoi(d);//string converts to int
 float num=stof(d);//string convert to float
 long num=stol(d);//string converts to long

2 auto 

C++ introduces auto and decltype keywords to implement type derivation. In the C++11 standard syntax, auto is defined as the type of automatic inferring variables.auto can automatically select a matching type for this variable according to the type of the variable's initial value when declaring a variable.

2.1 Use principle

  • The variable declared by auto must be initialized, otherwise the compiler cannot judge the type of the variable. This is similar to the const keyword.
  • auto cannot be declared as a return value, auto cannot be used as a formal parameter, auto cannot be modified as a template parameter.

2.2 Application examples

int x = 0;
 auto * a = &x;// a -> int * ,auto is deduced as int
 auto b = &x;// b -> int * ,auto is deduced as int*
 auto & c = x;// c -> int & ,auto is deduced as int
 auto d = c;//d -> int,auto is deduced as int When the expression is a reference type, auto will discard the reference type and directly deduce it into the original type int
 const auto e = x; // e -> const int ,auto is deduced as int
 auto f = e; // f -> int ,auto is deduced as int. When the expression has a const attribute, auto will discard the const attribute.
 const auto& g = x;//g -> const int& ,auto is deduced as int When auto is combined with reference, the derivation result of auto retains the expression const attribute
 auto& h = g;// h -> const int& ,auto is derived as const int

(1) When not declared as a reference or pointer, auto will ignore the reference type and const and volatile limitations on the right of the equal sign.

(2) When declared as a reference or pointer, auto will retain the reference to the right of the equal sign and the const and volatile attributes on the const and volatile attributes on the right of the equal sign.

(3) The auto keyword is more suitable for long and complex types and the scope of variables that are dedicated to use, making the program clearer and easier to read.

std::vector<int> vect;
  for(auto it = (); it != (); ++it)
  { //The type of it is std::vector<int>::iterator
     std::cin >> *it;
 }

(4) Save variable declaration of lambda expression type

auto ptr = [](double x){return x*x;};//The function object of type std::function<double(double)>

2.3 Limitations of use

(1) auto cannot be used for function parameters

void func(auto a = 1){}

(2) auto cannot be used for non-static member variables

class A
 {
   auto a = 1; // error, auto cannot be used as a non-static member variable in class
   static auto b = 1; // error, it has nothing to do with auto, normal static int b = 1 also cannot
   static const auto c = 1; // OK
 };

(3) Auto needs to be initialized immediately

auto a; //ERROR The auto type cannot be derived, the initial value is required

(4) When auto defines multiple variables on one line, the derivation of each variable cannot produce ambiguity, otherwise the compilation will fail

auto d = 0, f = 1.0; // error, 0 and 1.0 are different types, and they are ambiguity for the compiler and cannot be deduced.

(5) Auto cannot define an array, but pointers can be defined.

int arr[10] = {0};
 auto aa = arr; //ok ,aa -> int *;
 auto rr[10] = arr;//error,auto cannot define array

(6) Auto cannot deduce template parameters

vector<int> d;
 vector<auto> f = d; // error, auto cannot deduce template parameters

3 decltype

decltype is a new keyword added to C++11. It is the same as the function of auto, and is used toCompilation periodPerform automatic type derivation. decltype is used to deduce expression types, which is only used by the compiler to analyze the types of expressions, and the expression will not actually perform operations.

3.1 Syntax format

decltype(exp)

During the compilation period, the type of expression exp is derived.

3.2 Usage rules

  • If exp is an expression not surrounded by brackets (), or a class member access expression, or a separate variable, the type of decltype(exp) is the same as exp;
  • If exp is a function call, the type of decltype(exp) is the same as the type of the function return value;
int& fun_int_l(void);//lvalue
 int&& fun_int_rr(void);//RV
 int fun_int(void);//Pure rvalue
 int x = 0;
 decltype(fun_int_l()) a1 =x;//a1 -> int&
 decltype(fun_int_rr()) b1 =0;//b1 -> int&&
 decltype(fun_int()) c1 =0;//a1 -> int
  • If exp is an lvalue, or is surrounded by brackets (), the type of decltype(exp) is the lvalue reference of exp. Assuming that exp is T, then the type of decltype(exp) is T&;
int x = 0;
decltype(x) y = 1;           // y -> int
decltype(x + y) z = 0;       // z -> int
const int& i = x;
decltype(i) j = y;           // j -> const int &
const decltype(z) * p = &z;  // *p  -> const int, p  -> const int *
decltype(z) * pi = &z;       // *pi -> int      , pi -> int *
decltype(pi)* pp = &pi;      // *pp -> int *    , pp -> int * *

3.3 The differences between decltype and auto

auto can only deduce the type that the variable should have based on the initialization expression of the variable. If you want to obtain the type from an expression, but do not want the new variable to have the same value as this expression, auto does not apply at this time.

References:

【1】Detailed explanation of C++ auto usage and application_Code Turtle's Blog-CSDN Blog_c++ auto

【2】C++11 auto_aimalove's blog - CSDN blog 

【3】C++ Interview Eight-Legend Articles_51CTO Blog_java Interview Eight-Legend Articles