• decltype 인수로 받는 expression의 data type으로 변수를 만들 때 사용

    double x;
    int n;
    decltype(x*n) var1; //double
    decltype(&x) pt1; //double*
    
    • in template
    template<typename U, typename V>
    void func(U a, V b){
    	decltype(a+b) ab = a+b; //이런식으로 사용
    }
    
  • Trailing return type 함수의 반환형을 함수 이름 뒤쪽에 표시 *만약 decltype(a+b) func(U a, V b) 이렇게 하면 a,b를 몰라서 에러 *따라서 뒤쪽에 써서 알 수 있도록

    template<typename U, typename V>
    auto func(U a, V b)->decltype(a+b)
    {
    	return decltype(a+b); //이런식으로 사용
    }
    
  • Scoped Enumerations 기존의 enum: 전부 전역 변수이다→ 겹치는 이름이 있으면 안된다 enum class cenum {RED, GREEN, BLUE, VALUE}; enum struct senum {CYAN, MAGENTA, YELLOW, KEY, VALUE} 이러면 cenum::VALUE, senum::VALUE 이렇게 사용 가능

  • In class initialization

    class myclass{
    	int a=10; //원래는 안됐다!
    	double b=1.0;
    public:
    	~~
    };
    생성자에서 a,b를 받지 않으면 10, 1.0이 들어간다.
    
  • The rvalue reference

    • lvalue 변수 이름, dereferenced pointer 주소를 알 수 있다.

    • rvalue 주소를 알 수 없는 것

      int *pt = new int;
      int *pt = &(new int); //invalid! 
      const int b = 101; //101의 주소는 모른다! rvalue
      b= 500; //const라서 안되지만 &b는 valid
      x=y; //y또한 lvalue이다.
      
      
    • lvalue reference int &r = x 와 같은 것

    • rvalue reference int && r1 = 13; int && r2 = x+y; 의미: 주소를 얻을 수 있는 임시 공간에 저장함 복사가 일어나지 않는다.

      • Move Semantic memory copy를 없애는 문법 ex) vector<string> vstr(200000,”sdaf1ljk134ljk….”) vector<string> vstr2(vstr); //너무오래걸린다
      class Useless{
      private: int n; char*pc;
      public:
      	Useless(): n(0), pc(nullptr){}
      	Useless(const Useless &f):n(f.n), pc(new char[n]) {~~} 
      	//deepcopy
      	Useless(Useless &&f):n(f.n)
      	{
      		pc=f.pc; 
      		f.pc=nullptr;
      		f.n=0;
      	}
      	//move constructor: transfer the ownership
      };
      
      Useless one(10);
      Useless two = one; //copy constructor
      Useless four(one + two); //+operator, move constructor 
      
      • Assignment operator
      Useless& Useless::operator=(const Useless &f){~~}
      Useless& Useless::operator=(Useless&&f){
      	if(this == &f) return *this;
      	delete[] pc;
      	n = f.n; 
      	pc = f.pc; 
      	f.pc = nullptr;
      	f.n = 0;
      	return *this;
      }
      	
      
      • Forced Move move with lvalues
      #include <utility>
      ...
      four = std::move(one);
      
    • 사실 Move sematics는 STL에는 전부 정의되어 있으므로 신경쓰지 말자

  • C++11

    • 자동으로 만들어주는 함수들
      • default constructor
      • copy constructor
      • copy assignment operator
      • destructor
      • move constructor move assignment operator 이 둘은 사용자가 파괴, 복사, 복사대입을 만들면 안만들어줌
  • Lambda Functions unnamed temporary function function object 사용시 편하다.

    bool f3(int x){return x%3==0;}
    bool f13(int x){return x%13==0;}
    ...
    vector<int>numbers(1000);
    generate(numbers.begin(), numbers.end(),rand);
    int count3 = count_if(numbers.begin(), numbers.end(), f3);
    //단순한데도 정의와 사용되는 곳이 멀다!
    //functor을 클래스로 정의해도 길어질 뿐이다!
    
    //lambda function version
    //[](int x){return x%3==0;}
    //no return type
    //automatic type deduction
    int count13 = count_if(numbers.begin(),numbers.end(),
    											 [](int x){ return x%13==0;});
    
    //auto type deduction: 항상 작동은 아님(return 하나일때만)
    //trailing-return-value-syntax 사용
    [](double x} -> double {int y=x; return x-y;}
    
    
    • pros
      • Proximity: 사용되는곳과 정의되는 곳이 가깝다.

      • Brevity: 간결성

        • Lambda에 이름을 붙여서 여러번 쓸 수 있다. audo mod3 = [](int x){return x%3==0;}
      • Capability 대괄호 안에 여러 가지를 넣을 수 있다.

        Untitled

  • Variadic Templates 템플릿에 가변 인수를 사용할 수 있다.

  • cpp에서 …은 문법이다!

    • template<typename…Args> void show_list(Args…args){~}

    show_list(’S’,80,”Sweet”,4.5); 가능 이러면 args에 {’s’, 80, “Sweet””, 4.5}가 들어감. - Recursion? 종료조건을 템플릿으로 설정해 놓았다.

      ![Untitled](<https://s3-us-west-2.amazonaws.com/secure.notion-static.com/7673f877-b681-4fcd-992f-e81c9f86d7d6/Untitled.png>)