"C + + Academy" 0816-Reference wrapper/functor/escape character R "()"/using alias/template meta-programming than recursive optimization/smart pointers/multithreading/static assertions and debugging skills requirements assert

Source: Internet
Author: User

Reference Wrapperstd::ref ( variable )
#include <iostream>template<class t>void com (T arg)//template function, reference not valid, reference wrapper {std::cout << "com =" << &arg << "\ n"; arg++;} void Main () {int count = 10;int  & rcount = count;com (count); Std::cout << Count << std::endl;//std::ref (variable), function template, reference wrapper//com (Std::ref (count)), COM (rcount), std::cout << "main=" << &rcount << "\ n"; std: : cout << Count << std::endl;std::cin.get ();}

Imitation functions

Referencing the intrinsic function binding mechanism

#include <iostream> #include <functional>//processing function using namespace std;using namespace std::p laceholders;// Functor, create a function pointer, reference a struct inside or a class inside a public function struct mystruct{void add (int a) {cout << a << Endl;} void Add2 (int a,int b) {cout << a +b<< Endl;} void Add3 (int a, int b,int c) {cout << a + b +c<< endl;}}; void Main () {MyStruct struct1;//creates a function pointer, class struct, data private, code share//function by calling, calling to pass the object name to differentiate void (mystruct::* p) (int) = &mystruct: : Add;cin.get ();} int main1 () {mystruct Struct1;//auto automatic variable, address, function pointer, bind binding//first argument refers to intrinsic function, binds an entity object, auto Func = bind (&mystruct::add, &struct1, _1);//a parameter Auto Func2 = Bind (&mystruct::add2, &struct1,_1, _2);//two parameters Auto Func3 = Bind (& MYSTRUCT::ADD3, &struct1, _1, _2,_3);//Three parameters Func (+); Func2 (10, 20, 30);//will only receive the first 2 parameters Func2 (10, func3); Cin.get (); return 0;}
Escape CharacterR"()"
#include <iostream> #include <string> #include <stdlib.h>void main () {std::string path =r "(" C \ Program Files\tencent\qq\qqprotect\bin\qqprotect.exe ")";//r "()"   brackets Remove the escape character system (PATH.C_STR ()); System (" Pause ");}

Using aliases

#include <iostream>namespace space   //isolate template, avoid conflicts {Template<class t> using ptr = t*;//template shorthand}int Add (int A, int b) {return a + B;} typedef  INT (*add) (int a, int b), using  FUNC = Int (*) (int a, int b);//alias using  CO = std::ios_base::fmtflags;//u Sing can only be used for shorthand data type void Main () {ADD p=add;std::cout<<p (1, 2) <<std::endl; Func func = Add;std::cout << func (1, 2) << std::endl;//space::p tr<int> pint (new int);//std::cout &L t;< *pint << "   " << Pint << std::endl;std::cin.get ();
template meta-programmingthan recursive optimization
#include <iostream>//the runtime consumes time, optimizes template<int n>struct data{enum {res =data<n-1>::res+ during compilation data<n-2>::res};}; Template<>struct Data<1>{enum {res = 1};}; Template<>struct Data<2>{enum {res= 1};};/ /recursive Fibonacci sequence//1 1 2 3 5 8int getdata (int n) {if (N==1 | | n==2) {return 1;} else {return GetData (n-1) + GetData (n-2);} }void Main () {const int myint = 40;int num = data<myint>::res;//<> internal cannot have variables std::cout << num << std:: Endl;std::cout << GetData (+) << std::endl;std::cin.get ();} The main idea of the////is to use the template special mechanism to achieve the compile-time conditional selection structure, using recursive template to implement the compile-time loop structure, and the template meta-Program is interpreted by the compiler at compile time. Pros and cons////by moving the calculation from the runtime to the compile time, do as much work as possible before the result program starts, and eventually get faster programs. That is to say, the advantage of template meta-programming is:////1. In exchange for excellent run-time performance at compile time (typically for higher performance for performance-critical numerical calculations). In general, the number of times a meaningful program runs (or service time) is always much more than the number of compilations (or compile time). 2. Provide compile-time type calculation, usually this is the place where template meta-programming shines brilliantly. Template meta-programming techniques are not all advantages:////1. Code readability is poor, and describing the algorithm in a class template may be a bit abstract. 2. Debugging difficulties, the meta-Program is executed at compile time, there is no debugger for single-step tracking meta-Program execution (to set breakpoints, view data, etc.). The programmer can only do is wait for the compilation process to fail, and then manually decipher the compiler pouring to the screen error message. 3. Long compilation time, usually with template meta-Program generated code size thanThe general program is big,////4. Portability is poor, and the level of support for template meta-programming is different for advanced template features. 

Smart Pointers
#include <iostream>void main1 () {//auto_ptr;for (int i = 0; i < 10000000; i++) {Double *p = new double;//assigns a memory Std to the pointer ::auto_ptr<double> autop (P);//create smart pointer management pointer p pointing to memory//smart pointer//delete p;} Std::cin.get ();}

c++11 Smart Pointer

#include <iostream> #include <memory>//memory void Main () {for (int i = 0; i < 10000000;i++)    {//new pointer, New Array std::unique_ptr<double> pdb (new double);//double *p = new double;    } Std::cin.get ();}

Multithreading
 #include <thread> #include <iostream> #include <windows.h> #include <vector>using namespace std;using namespace std::this_thread;void msg () {messageboxa (0, "12345", "678910", 0);} void msga (int num) {std::cout << get_id () << "num=" << num << Std::endl;} void Main1 () {//thread::hardware_concurrency thread Auto n = thread::hardware_concurrency () std::cout << n << std   :: endl;//Gets the current thread number std::cout << "thread=" << get_id () << Std::endl;   Thread Thread1 (msg);//Create multithreaded Thread thread2 (msg); Thread1.join ();//Start execution Thread2.join (); Std::cin.get ();}  void Main2 () {vector<thread *> threads;for (int i = 0; i <; i++) {Threads.push_back (new Thread (msg));//Create Thread}for (Auto Th:threads) {Th->join ();} Std::cin.get ();} void Main () {vector<thread *> threads;for (int i = 0; i <; i++) {Threads.push_back (new thread (msga,i));//Create Thread}FO R (Auto th:threads) {th->join ();} Std::cin.get ();} 

Static assertions and requirements for debugging skills assert

#include <stdio.h> #include <assert.h> #include <iostream>using namespace std; #define  N 10void Main () {int num = 100;cout << num << endl;cout << __file__ << endl;cout << __line__ << Endl;cout << __date__ << endl;cout << __time__ << endl;cout << __function__ << endl;ci N.get ();} #define Mvoid main1 () {   char  num = ten;   Static_assert (sizeof (num) >= 1, "num valiued");   ASSERT (Num >= 4, "num>4");//byte >4#ifdef  M  //static_assert (sizeof (NUM) >= 4, "YinCheng error"); endif//debug code, Quick Code error in which line}






Copyright notice: This blog all articles are original, welcome to Exchange, welcome reprint, reprint do not tamper with the content, and indicate the source, thank you!

C + + Institute 0816-Reference wrapper/functor/escape character R "()"/using alias/template meta-programming than recursive optimization/smart pointers/multithreading/static assertions and debugging skills requirements assert

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.