[C++ Korea] Effective Modern C++ Study, Item 1 - 3

65
Effective Modern C++ Study C++ Korea

Transcript of [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Page 1: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

Page 2: [C++ Korea] Effective Modern C++ Study, Item 1 - 3
Page 3: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea3

Page 4: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

template<typename T>void f(ParamType param);

f(expr);

4

Page 5: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

template<typename T>void f(const T& param);

int x = 0;f(expr);

// ParamType은 const T&

5

Page 6: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea6

Page 7: [C++ Korea] Effective Modern C++ Study, Item 1 - 3
Page 8: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

template<typename T>void f(T& param);

int x = 27;const int cx = x;const int& rx = x;

f(x); // T의 타입은 int// param의 타입은 int&// T의 타입은 const int// param의 타입은 const int&// T의 타입은 const int// param의 타입은 const int&

f(cx);

f(rx);

8

Page 9: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

template<typename T>void f(const T& param);

int x = 27;const int cx = x;const int& rx = x;

f(x); // T의 타입은 int// param의 타입은 const int&// T의 타입은 int// param의 타입은 const int&// T의 타입은 int// param의 타입은 const int&

f(cx);

f(rx);

9

Page 10: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

template<typename T>void f(T* param);

int x = 27;const int* px = &x;

f(&x); // T의 타입은 int// param의 타입은 int*

// T의 타입은 const int// param의 타입은 const int*

f(px);

10

Page 11: [C++ Korea] Effective Modern C++ Study, Item 1 - 3
Page 12: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea12

Page 13: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

template<typename T>void f(T&& param);

int x = 27;const int cx = x;const int& rx = x;

f(x); // x는 Lvalue, T의 타입은 int&// param의 타입 또한 int&

// cx는 Lvalue, T의 타입은 const int&// param의 타입 또한 const int&

f(cx);

f(rx); // rx는 Lvalue, T의 타입은 const int&// param의 타입 또한 const int&

f(27); // 27은 Rvalue, T의 타입은 int// 따라서 param의 타입은 int&&

13

Page 14: [C++ Korea] Effective Modern C++ Study, Item 1 - 3
Page 15: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea15

Page 16: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

template<typename T>void f(T param);

int x = 27;const int cx = x;const int& rx = x;

f(x); // T와 param의 타입은 둘 다 int

// T와 param의 타입은 둘 다 intf(cx);

f(rx); // T와 param의 타입은 둘 다 int

param은 cx 및 rx와 다른 오브젝트!따라서 param이 무엇이든 cx와 rx는 수정할 수 없음

16

Page 17: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea17

Page 18: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

template<typename T>void f(T param);

const char* const ptr =“Fun with pointers”;

f(ptr);

18

Page 19: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

template<typename T>void f(T param);

const char* const ptr =“Fun with pointers”;

f(ptr);

19

Page 20: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

template<typename T>void f(T param);

const char* const ptr =“Fun with pointers”;

f(ptr);

20

Page 21: [C++ Korea] Effective Modern C++ Study, Item 1 - 3
Page 22: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

const char name[] = “J. P. Briggs”; // name의 타입은 const char[13]const char* ptrToName = name; // 배열이 포인터로 붕괴됨

여기서, const char* 타입인 ptrToName은const char[13] 타입인 name으로 초기화됨

const char*와 const char[13]은 서로 같은 타입이 아니지만,배열 – 포인터 붕괴 규칙으로 인해 컴파일됨

22

Page 23: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

template<typename T>void f(T param);

const char name[] = “J. P. Briggs”;

f(name); // T와 param에 대해 어떤 타입으로 추론될까?

23

Page 24: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

f(name); // name은 배열이지만, T는 const char*로 추론됨

24

Page 25: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

template<typename T>void f(T& param);

const char name[] = “J. P. Briggs”;

f(name); // T와 param에 대해 어떤 타입으로 추론될까?

25

Page 26: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

template<typename T, std::size_t N>constexpr std::size_t arraySize(T(&)[N]) noexcept {

return N;}int keyVals[] = {1, 3, 7, 9, 11, 22, 35};int mappedVals[arraySize(keyVals)];

std::array<int, arraySize(keyVals)> mappedVals;

// constexpr을 선언하면// 컴파일하는 동안 작업을 처리함// noexcept를 선언하면// 컴파일러가 좋은 코드를 생성함

// 모던 C++에서는 std::array를 사용하는 것이 좋음

26

Page 27: [C++ Korea] Effective Modern C++ Study, Item 1 - 3
Page 28: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

void someFunc(int, double); // someFunc는 함수// 타입은 void(int, double)

template<typename T>void f1(T param);template<typename T>void f2(T& param);

// f1은 값에 의한 전달

// f2는 레퍼런스에 의한 전달

f1(someFunc);f2(someFunc);

// 타입은 void(*)(int, double)

// 타입은 void(&)(int, double)

28

Page 29: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea29

Page 30: [C++ Korea] Effective Modern C++ Study, Item 1 - 3
Page 31: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

template<typename T>void f(ParamType param);f(expr);

템플릿 타입 추론

auto x = 27;const auto cx = x;const auto& rx = x;

auto 타입 추론

직접 매핑 (Direct Mapping)

31

Page 32: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

template<typename T>void func_for_x(T param);func_for_x(27);template<typename T>void func_for_cx(const T param);func_for_cx(x);template<typename T>void func_for_rx(const T& param);func_for_rx(x);

auto x = 27;(타입 지정자 : auto)

const auto cx = x;(타입 지정자 : const auto)

const auto& rx = x;(타입 지정자 : const auto&)

32

Page 33: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea33

Page 34: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

auto x = 27; // 경우 3 // x는 포인터 또는 레퍼런스가 아님// 경우 3// cx는 포인터 또는 레퍼런스가 아님// 경우 1// rx는 유니버셜 레퍼런스가 아닌 레퍼런스임

const auto cx = x;

const auto& rx = x;

34

Page 35: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

auto&& uref1 = x; // x는 int이며 Lvalue// 따라서 uref1의 타입은 int&// cx는 const int이며 Lvalue// 따라서 uref2의 타입은 const int&// 27은 int이며 Rvalue// 따라서 uref3의 타입은 int&&

auto&& uref2 = cx;

auto&& uref3 = 27;

35

Page 36: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

// func1의 타입은// void(*)(int, double)

const char name[] =“R. N. Briggs”;

// name의 타입은 const char[13]

auto arr1 = name;auto& arr2 = name;

void someFunc(int, double);

auto func1 = someFunc;

auto& func2 = someFunc;

// arr1의 타입은 const char*// arr2의 타입은 const char(&)[13]

// func2의 타입은// void(&)(int, double)

// someFunc는 함수// 타입은 void(int, double)

36

Page 37: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea37

Page 38: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea38

Page 39: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea39

Page 40: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

auto x = {11, 23, 9};

template<typename T>void f(T param);

// x의 타입은 std::initializer_list<int>

// 매개변수가 있는 템플릿의 선언은// x의 선언와 동일함

f({11, 23, 9}); // 오류! T의 타입을 추론할 수 없음

40

Page 41: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

template<typename T>void f(std::initializer_list<T> initList);

f({11, 23, 9}); // T는 int로 추론// initList의 타입은 std::initializer_list<int>

41

Page 42: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

auto createInitList(){

return {1, 2, 3};}

// 오류 : 타입을 추론할 수 없음

42

Page 43: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

std::vector<int> v;…auto resetV =

[&v](const auto& newValue) { v = newValue; };…resetV({1, 2, 3}); // 오류 : 타입을 추론할 수 없음

43

Page 44: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea44

Page 45: [C++ Korea] Effective Modern C++ Study, Item 1 - 3
Page 46: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

const int i = 0; // decltype(i)는 const intbool f(const Widget& w); // decltype(w)는 const Widget&

// decltype(f)는 bool(const Widget&)struct Point {

int x, y;};

// decltype(Point::x)는 int// decltype(Point::y)는 int

Widget w; // decltype(w)는 Widgetif (f(w)) // decltype(f(w))는 bool

46

Page 47: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

template<typename Container, typename Index>auto authAndAccess(Container& c, Index i)

-> decltype(c[i]){

authenticateUser();return c[i];

}함수 이름 전에 있는 auto는 타입 추론과 아무 관련 없음

47

Page 48: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea48

Page 49: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea49

Page 50: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

template<typename Container, typename Index>auto authAndAccess(Container& c, Index i){

authenticateUser();return c[i];

}

50

Page 51: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea51

Page 52: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

std::deque<int> d;…authAndAccess(d, 5) = 10; // d[5] = 10, 컴파일 오류 발생!

52

Page 53: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

template<typename Container, typename Index>decltype(auto) authAndAccess(Container& c, Index i) {

authenticateUser();return c[i];

}53

Page 54: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

Widget w;const Widget& cw = w;

auto myWidget1 = cw;

decltype(auto) myWidget2 = cw;

// auto 타입 추론// myWidget1의 타입은 Widget// decltype 타입 추론// myWidget2의 타입은// const Widget&

54

Page 55: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

template<typename Container, typename Index>decltype(auto) authAndAccess(Container& c, Index i);

55

Page 56: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

std::deque<std::string> makeStringDeque(); // 팩토리 함수

auto s = authAndAccess(makeStringDeque(), 5);

56

Page 57: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

template<typename Container, typename Index>decltype(auto) authAndAccess(Container&& c, Index i);

57

Page 58: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

template<typename Container, typename Index>decltype(auto) authAndAccess(Container&& c, Index i){

authenticateUser();return std::forward<Container>(c)[i];

}

58

Page 59: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

template<typename Container, typename Index>auto authAndAccess(Container&& c, Index i)-> decltype(std::forward<Container>(c)[i]){

authenticateUser();return std::forward<Container>(c)[i];

}

59

Page 60: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea60

Page 61: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea61

Page 62: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

declrtype(auto) f1(){

int x = 0;return x;

}

declrtype(auto) f2(){

int x = 0;return (x);

}

// decltype(x)는 int// 따라서 f1는 int를 반환

// decltype((x))는 int&// 따라서 f2는 int&를 반환

62

Page 63: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

(expr == NAME*) ? AS DECLARED :(expr == lvalue) ? T& :

// rvalue(expr == xvalue) ? T&& : T // prvalue

* NAME: plain, unparenthesised variable, function - parameter, class member access

63

Page 64: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea

* xvalue: - function call where the function’s return value is declared as and rvalue referencee.g. std:move(x)

- static cast to an rvalue referencee.g. static_cast<A&&>(a)

- a member access of an xvaluee.g. static_cast<A&&>(a)).m_x

* prvalue: all other rvalues than above cases

64

Page 65: [C++ Korea] Effective Modern C++ Study, Item 1 - 3

Effective Modern C++ StudyC++ Korea65