decltype 인수로 받는 expression의 data type으로 변수를 만들 때 사용
double x;
int n;
decltype(x*n) var1; //double
decltype(&x) pt1; //double*
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; 의미: 주소를 얻을 수 있는 임시 공간에 저장함 복사가 일어나지 않는다.
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
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;
}
#include <utility>
...
four = std::move(one);
사실 Move sematics는 STL에는 전부 정의되어 있으므로 신경쓰지 말자
C++11
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;}
Proximity: 사용되는곳과 정의되는 곳이 가깝다.
Brevity: 간결성
Capability 대괄호 안에 여러 가지를 넣을 수 있다.

Variadic Templates 템플릿에 가변 인수를 사용할 수 있다.
cpp에서 …은 문법이다!
show_list(’S’,80,”Sweet”,4.5); 가능 이러면 args에 {’s’, 80, “Sweet””, 4.5}가 들어감. - Recursion? 종료조건을 템플릿으로 설정해 놓았다.
