[c++ korea] effective modern c++ study item8~10 정은식

15
Effective Modern C++ Stu C++ Kor C++ Korea Effective Modern C++ Study Item: 8~10 정정정

Upload: -

Post on 28-Jul-2015

309 views

Category:

Engineering


2 download

TRANSCRIPT

Effective Modern C++ StudyC++ Korea

C++ KoreaEffective Modern C++ Study

Item: 8~10 정은식

Item 8 : Item 8: Prefer nullptr to 0 and NULL .

Effective Modern C++ StudyC++ Korea

Item 8: Prefer nullptr to 0 and NULL .C++98 에서 NULL pointer 로 만들기 위해 0 , NULL 이 존재 .

그치만 0 은 NULL 이 아니라 int 형이다 .

만약 포인터로 사용될 경우 마지못해 NULL 로 대체 된다 .

또한 NULL 은 정수 형태를 낼수 있는 문제점이 있다 .

평소에는 아무런 문제가 없겠지만 NULL 은 0,NULL 이 타입을 가지고 있다 .

그래서 C++11 에서는 nullptr 등장

3

Effective Modern C++ StudyC++ Korea

C++98 오버로딩 문제

void f(int);if(result == 0) void f(bool);void f(void*);f(0);f(NULL);

컴파일은 안되지만 f(NULL) 은 f(int) 호출 ( 네 ?) 비교문에 포인터 형태인지 정수형태인지 알수없다 .

해결법

f(nullptr) 사용 if(result ==nullptr)

4

Effective Modern C++ StudyC++ Korea

Func = 사용자가 정의한 함수 template<typename FuncType, typename MuxType, typename PtrType>auto lockAndCall (FuncType func, MuxType & mutex, PtrType ptr) -> decltype(func(ptr)){ MuxGuard g(mutex); return func(ptr);}auto result1 = lockAndCall(f1, f1m, 0); // error!...auto result2 = lockAndCall(f2, f2m, NULL); // error!...auto result3 = lockAndCall(f3, f3m, nullptr); // fine

5

Item 9 : Item 9: Prefer alias declarations to typedef s.

Effective Modern C++ StudyC++ Korea

별칭을 만드는건 C++ 98 에 typedef 존재

Typedef std::unique_ptr<std::unordered_map<std::string, std::string>> UptrMapSS

C++11 에서는 using 이라는걸 제공

using UPtrMapSS = std::unique_ptr<std::unordered_map<std::string, std::string>>;

또한 함수포인터도 처리 가능하다 .

7

Effective Modern C++ StudyC++ Korea

WHY C++98 은 template 안에 typedef 를 중첩하는거에 비해 C++ 11 에서 직접적인 메커니즘을 사용해서 쓴다 .

template<typename T>Struct MyAlloList{Typedef std::list<t,myalloc<T>> type;};

MyAllocList<Widget>::type lw;

template<typename T>class Widget {private:typename MyAllocList<T>::type list;};

8

Effective Modern C++ StudyC++ Korea

WHY Using 을 사용하면 ? Typedef 보다 간단하게 줄일수 있다 .template<typename T>using MyAllocList = std::list<T, MyAlloc<T>>;

MyAllocList<Widget> lw;

template<typename T>class Widget {private:MyAllocList<T> list;};

9

Item 10 : Item 10: Prefer scoped enum s to unscoped enum s.

Effective Modern C++ StudyC++ Korea

enum Color { black, white, red }; // black, white, red are

Color a=black;

if(a < 14.5) //a 는 int -> double 형변환

C++98 에서 enum 은 정수형으로 선언되어 문제가 발생

enum Color {RED, GREEN, BLUE};enum Feelings {EXCITED, MOODY, BLUE};

또한 열거형 안에 내용이 중복으로 선언되면 안된다 .

11

Effective Modern C++ StudyC++ Korea

그래서 C++11 에서 enum class 사용해서 해결

Enum class Color { black, white, red }; // black, white, red

Color a=black; //error black 는 열거형 범위에 없음

Color b=Color::black; if(b < 14.5) // error 에러

enum class Color {RED, GREEN, BLUE};enum class Feelings {EXCITED, MOODY, BLUE};

중복 해결

12

Effective Modern C++ StudyC++ Korea

enum class Mood : char{ EXCITED=1, MOODY=2, BLUE=3 };void k(Mood a){if(a==Mood::EXCITED){cout<<"EXCITED"<<endl;}}int main(){Mood m = Mood::EXCITED;k(m);}

13

Effective Modern C++ StudyC++ Korea

참조 사이트

http://egloos.zum.com/sweeper/v/2996830

http://www.cprogramming.com/c++11/c++11-nullptr-strongly-typed-enum-class.html

14

Effective Modern C++ StudyC++ Korea

정리

Item 8 : null pointer 사용하고 싶을떄 0 ,NULL 대신 nullptr 을 사용하자

Item 9 : typdef 보다 using 로 별칭을 만들자 .

Item 10 : enum class 를 쓰자

15