Moment For Technology

New features of C++2.0 | more challenges in August

Posted on Dec. 3, 2022, 8:30 a.m. by Madeleine Burton
Category: The back-end Tag: The back-end

1, syntax sugar auto

In previous versions, the auto keyword was a storage type descriptor indicating dynamic or static storage, corresponding to the static keyword.

The new C++2.0 standard introduces the auto type specifier, which allows the compiler to analyze the type of an expression for us.

In simple terms, auto automatically calculates the variable type through the initial value, that is, automatic type inference.

Note the following points when using auto:

1. Variables defined by auto must have initial values;

2. Auto can declare multiple variables in a statement, but all variables in the statement must have the same initial basic data type, because a statement can only have one basic data type;

3. Auto deduces the reference type as the reference object, that is, the type of the reference object is auto.

4. Auto generally ignores the const at the top level, and the const at the bottom level is retained. If you want const at the top level, you need to display the declaration.

5. Auto If the variable is set to a reference type, the binding literal cannot be referenced for an unusual amount (reference knowledge);

6. Auto cannot be used for the function parameter !!!! Such as void f (auto x);

The code tests are as follows:

#include "iostream"
#include "string"
double f(int a,int b){return 0; }auto pf=f;// Auto must have initial values
int main(int argc,char const *argv[]){
    auto a=1;
    auto b=1.2;
    auto ptr=a;
    std::string s="* * * * * *";
    auto s_begin=s.begin(a);auto c=s;

    int *p=0,x=1;
    auto i=0,*pos=i;//true: I is an integer, p is an integer pointer;
    / / auto iz = 0, PI = 2.56; // Error: Iz type is inconsistent with PI type

    int ii=0,ir=ii;
    auto a=ir;  // A is an integer and is equal to zero

    const int ic=0,icr=ic;
    auto m=ic;/ / integer
    auto n=icr;/ / integer
    auto o=ii;// An integer pointer
    auto p=ic;// A pointer to an integer constant

    const auto y=ic;/ / y is const int

    auto _r=ic; // Reference to IC
    //auto _s=42; //error: binding literals cannot be referenced for non-regular quantities
    const auto _s=42;// Constant reference

    const int h=42;
    //int h=42; //error: binding literals cannot be referenced for non-regular quantities
    
    return 0;
}

Copy the code

2, syntax sugar raw literal (raw string)

In programming, if you encounter a string "\n", it means a newline, that is, "\n" means a character -- a newline. So what if we want to make a '\' a '\'? In C, you write "\\n" like this. It's easy to write a string like this, but what if we have multiple \ and letter combinations in a string and we just want it to be literal? This is where the original literal comes in.

Defined as R "XXX (raw string) XXX";

Note: the XXX characters after R must be exactly the same. Otherwise, the compiler will report an error.

The code tests are as follows:

#includeiostream
#includestring 
int main(int argc,char const *argv[])
{
    std::string str = "D:\C++\test.cpp";
    std::cout  str  std::endl;
    std::string str = "D:\\C++\\test.cpp";
    std::cout  str1  sdt::endl;
    sdt::string str = R"(D:\C++\test.cpp)";
    sdt::cout  str2  sdt::endl;

    return 0;
}

Copy the code

3. Scope-based for loops

The new C++2.0 standard introduces a simpler for statement that traverses all elements of a container or other sequence; The grammatical form is as follows:

f o r ( d e c l a r a t i o n : e x p r e s s i o n ) s t a t e m e n t for(declaration:expression)\\statement\\

1. Expression is an instance object of a sequence or container

2. Declaration is a specific type inside a container or sequence

Statement Specifies the expression to be operated on

Test code is as follows:

#include iostream
#include vector
int main(a)
{
    std::vectorstd::pairint.int t(10,std::make_pair(1.2));
    for (auto x:t)
    {
        std::cout  x.first "* *"x.secondstd::endl;
    }
    
    return 0;
}
Copy the code

Note:

For (auto x:t) is passed by reference

2, auto type derivation type, of course, can also display declarations

X = STD ::pairint,int;

4. List initialization

C++2.0 expands the use of bracketed lists (initializing lists), which can be initialized with or without =. As follows:

int x={5};
double y {2.75};
short quar [5] {4.5.2.76.1};
Copy the code

In addition, the list initialization syntax can also be used in new expressions:

int *ar = new int [4] {6.6.6.6};
Copy the code

The constructor can also be called with a list enclosed in braces (instead of parentheses) when creating an object:

class test{
    private:
    	int x;
    	int y;
    	int z;
    public:
    	test(int M,int N,int Q):x(M),y(N),z(Q){};
};

test t1{1.2.3};
test t2={2.3.4};// The constructor is called
Copy the code

Benefits of list initialization:

The initializer list syntax prevents narrowing by disallowing assigning a value to a numeric variable that cannot store it. Regular initialization allows the programmer to perform operations that may not make sense, such as:

char c1=11111;/ / warning
char c2={11111};/ / error
char c3={100};/ / allow
Copy the code

STD ::initialzer_list applies to a normal function argument:

#include "iostream"
#include "string"
#include "initializer_list"
using namespace std;
void test(initializer_listint i){
    int addr=0;
    for(auto pos=i.begin(a); pos! =i.end(a); ++pos){ cout"pos="*posendl; }}int main(int argc,char const *argv[]){
    test({1.2.3.4.5});
    
    return 0;
}
Copy the code
Search
About
mo4tech.com (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.