23
Effective Modern C++ Study C++ Korea nullptr NULL Speaker : Yun Seok-joon ( [email protected] )

[C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준

Embed Size (px)

Citation preview

Page 1: [C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준

Effective Modern C++ StudyC++ Korea

nullptr NULL

Speaker : Yun Seok-joon ( [email protected] )

Page 2: [C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준

Prefer nullptr to 0 and NULL.

Page 3: [C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준

1. C++98에서는왜NULL을 ?

2. Overloading Resoulution Rule

3. C++11 nullptr

4. template + decltype 예제

Page 4: [C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준

1. C++98에서는왜 NULL을 ?

Page 5: [C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준

Effective Modern C++ StudyC++ Korea

#define NULL 0

5

왜 ?

다른 방법이 없다.

포인터와 정수형의 Overloading

자제

문제가 생길 수있으니깐

문제가 있는데왜 사용을 ?

Page 6: [C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준

Effective Modern C++ StudyC++ Korea

Overloading Resolution Rule 때문이야.

6

#define NULL_PTR 0L

void func(int) {};void func(bool) {};void func(void*) {};

func(0); // calls func(int)func(NULL); // calls func(int)func(NULL_PTR); // calls func(int)func((void*)NULL); // calls func(void*)

Page 7: [C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준
Page 8: [C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준

Effective Modern C++ StudyC++ Korea8

• 같은 이름을 가진 함수를 중복으로 선언

• 조건

- 인자 목록만 다르면 가능 (타입 종류 및 순서)

- 인자 이름은 상관없다.

- return 타입도 상관없다.

Page 9: [C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준

Effective Modern C++ StudyC++ Korea9

1. 기본타입은 const와의 공존을 거부한다.

void Func(int);

void Func(const int);

// error C2084: func 'void Func(int)' already// has a body

天上天下唯我獨尊

Page 10: [C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준

Effective Modern C++ StudyC++ Korea10

2. Reference 와 Pointer는 const와의 공존이 가능하다.

void Func(int& a);void Func(const int& a);

void Func(int* a);void Func(const int* a);

Page 11: [C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준

Effective Modern C++ StudyC++ Korea11

3. 기본타입은 레퍼런스와의 공존도 거부한다.

당연히 const int& 역시 거부한다.

void Func(int);void Func(int&);void Func(const int&);

// error C2665: 'Func': none of the 3 overloads// could convert all the argument types

天上天下唯我獨尊

Page 12: [C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준

Effective Modern C++ StudyC++ Korea12

http://en.cppreference.com/w/cpp/language/overload_resolution

더 자세히 알고 싶으시다면,

아래 Link를 참고(내용이 많아요. 기회가 되면 번역해서 블로그에 올릴께요.)

Page 13: [C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준

3. C++11 nullptr

Page 14: [C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준

Effective Modern C++ StudyC++ Korea14

1. 모든 종류의 Raw Pointer로 암묵적 변환(Implicit casting)이 가능

nullptr은 int 타입이 아니며, std::nullptr_t 타입이다.

void func(int) {};void func(bool) {};void func(void*) {};

func(NULL); // calls func(int)

func(nullptr); // calls func(void*)

Page 15: [C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준

Effective Modern C++ StudyC++ Korea15

2. 가독성 (Readability) 향상

특히 auto랑 같이 사용한 경우

auto OBJ = FindObj();if (OBJ == 0)

auto OBJ = FindObj();if (OBJ == NULL)

auto OBJ = FindObj();if (OBJ == nullptr)

숫자형 이겠지 ?

(int 라던지…)

숫자형인가 ?

포인터인가?

누가봐도 포인터다.

그래도 NULL을 쓰면 안되는건 아닌데…

Page 16: [C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준

Effective Modern C++ StudyC++ Korea16

그래도 NULL을 쓰면 안되는건 아닌데…

왜 꼭 nullptr을 쓰라고 하지 ?

안쓰면 안되는 경우가 있나 ?

Page 17: [C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준

4. template + decltype 예제

Page 18: [C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준

Effective Modern C++ StudyC++ Korea

#define NULL 0int FuncSP(std::shared_ptr<Widget> SPW);double FuncUP(std::unique_ptr<Widget> UPW);bool FuncRP(Widget* RPW);

std::mutex MS, MU, MR;using LOCK = std::lock_guard<std::mutex>; // C++11 : 조금 더 고급진 typedef{

LOCK L(MS); // RAII 를 지원하는 lock_guard 로 Code를 고급지게auto RET = FuncSP(0);

}{LOCK L(MU);auto RET = FuncUP(NULL);

}{LOCK L(MR);auto RET = FuncUP(nullptr);

}

Page 19: [C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준

Effective Modern C++ StudyC++ Korea

template<typename FUNC,typename MUTEX,typename PTR>

auto LockAndFunc(FUNC F, MUTEX& M, PTR P) -> decltype(F(P)){

LOCK L(M);return F(P);

}

Page 20: [C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준

Effective Modern C++ StudyC++ Korea

template<typename FUNC,typename MUTEX,typename PTR>

decltype(auto) LockAndFunc(FUNC F, MUTEX& M, PTR P){

LOCK L(M);return F(P);

}

Page 21: [C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준

Effective Modern C++ StudyC++ Korea

auto RS = LockAndFunc(FuncSP, MS, 0); // errorauto RU = LockAndFunc(FuncUP, MU, NULL); // errorauto RR = LockAndFunc(FuncRP, MR, nullptr); // OK

//can’t convert int to std::shared_ptr<Widget>

O, NULL은 언제나 정수형으로 추론되기 때문에

절대로 std::shared_ptr<Widget> 같은 타입으로는 추론이 안된다.

Page 22: [C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준
Page 23: [C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준

Effective Modern C++ StudyC++ Korea23

• 0 과 NULL은 보다는 nullptr을 사용합시다.

• 정수형과 Pointer 타입의 Overloading을 피합시다.

http://devluna.blogspot.kr/2015/04/item-08-0-null-nullptr-mva-version.html

[email protected]