Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Case Analysis of right value reference and Mobile Construction of C++

Shulou Source: shulou.com Published: 2022-05-31 14:04:37 09月24日 Update

This article mainly introduces the relevant knowledge of "c++ rvalue reference and mobile construction example analysis". Xiaobian shows you the operation process through actual cases. The operation method is simple, fast and practical. I hope this article "c++ rvalue reference and mobile construction example analysis" can help you solve the problem.

Analysis of New Features of C++11

auto

Is now a type placeholder that tells the compiler that the actual type of the variable should be inferred from the initializer. Auto can be used when you want to declare variables in different scopes (e.g., namespaces, initializers within functions, initializers within for loops).

auto i = 42; // i is an int

auto l = 42LL; // l is an long long

auto p = new foo(); // p is a foo*

Using auto often means less code (unless you need a type of int that has only one word). When you want to traverse elements in an STL container, think about how you would write iterator code. The old-fashioned way is to do it with a lot of typedefs, and auto simplifies the process a lot.

std::map map;

for(auto it = begin(map); it != end(map); ++it)

{

}

You should note that auto is not the return type of a function, but you can replace it with auto, in which case the function must have a return value. Auto does not tell the compiler to infer the actual return type, it tells the compiler to look for the return type at the end of the function. In the example below, the composition of the return value of the function is determined by values of type T1 and type T2 after the + operator.

template

auto compose(T1 t1, T2 t2) -> decltype(t1 + t2)

{

return t1+t2;

}

auto v = compose(2, 3.14); // v's type is double

nullptr keyword

0 used to be the value of a null pointer, which has some drawbacks because it can be implicitly converted to an integer variable. The nullptr keyword represents the value type std::nullptr_t, which semantically can be understood as a null pointer. nullptr can be implicitly converted to null pointers of any type, as well as member function pointers and member variable pointers, and can also be converted to bool(with a value of false), but implicit conversion to integer variables no longer exists.

void foo(int* p) {}

void bar(std::shared_ptr p) {}

int* p1 = NULL;

int* p2 = nullptr;

if(p1 == p2)

{

}

foo(nullptr);

bar(nullptr);

bool f = nullptr;

int i = nullptr; // error: A native nullptr can only be converted to bool or, using reinterpret_cast, to an integral type

For backward compatibility, 0 can still be used as a null pointer value.

Interval-based cycles

C++11 enhances the functionality of the for statement to better support foreach paradigms for traversing collections. In the new form, the user can use for to iterate through C-style arrays, initialization lists, and containers where all non-members begin() and end() are overloaded.

This form of for is useful when you just want to get the elements of a collection/array to do something, not the index value, iterator, or element itself.

std::map map;

std::vector v;

v.push_back(1);

v.push_back(2);

v.push_back(3);

map["one"] = v;

for(const auto& kvp : map)

{

std::cout

Tags: Function type copy move pointer object code example point compile member time memory variable operator array element identity error problem Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Microsoft vpn NVidia Redmi