welcome to modern c++

26
WareValley http://www.warevalley.com

Upload: seok-joon-yun

Post on 13-Apr-2017

328 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Welcome to Modern C++

WareValleyhttp://www.warevalley.com

Page 2: Welcome to Modern C++

Overviewvector<vector<int>>

user-defined

literals thread_local

=default, =deleteatomic<T> auto f() -> int

array<T, N>

decltypevector<LocalType>

noexceptregex

initializer lists

constexpr

extern template

unordered_map<int, string>raw string literals

nullptr auto i = v.begin();

async

lambdas

[]{ foo(); }

template

aliases

unique_ptr<T>

shared_ptr<T>

weak_ptr<T>

thread, mutex

for (x : coll)

override,

final

variadic templatestemplate <typename T…>

function<>

promise<T>/future<T>

tuple<int, float, string>strongly-typed enums

enum class E {…};

static_assert(x)

rvalue references(move semantics)

delegating constructors

packaged_task<T>

Decucing Type

Concurrency

Performance

Smart Pointer

lambda expression

range-based for

initializer list

Page 3: Welcome to Modern C++

auto f() -> int

decltype

auto i = v.begin();

decltype(auto)

Page 4: Welcome to Modern C++

4

• 경우 1 : ParamType이 pointer 또는 reference 타입이지만,

universal reference 타입은 아닌 경우 (L-Value 및 R-Value와 다름)

• 경우 2 : ParamType이 universal reference 타입인 경우

• 경우 3 : ParamType이 pointer 또는 reference 타입이 아닌 경우

* 자세한 설명: http://devluna.blogspot.kr/2015/02/item-1-understand-template-type.html

Page 5: Welcome to Modern C++

5

int i = 10;double d = 3.14;

auto i = 10;auto d = 3.14;

C++98 C++11

• 자세한 설명: http://devluna.blogspot.kr/2015/02/item-2-understand-auto-type-deduction.html

Page 6: Welcome to Modern C++

6

C++98

C++11

for (std::vector<std::tuple<std::string, int, double>>::iterator IT = vtList.begin();IT != vtList.end(); IT++)

{…

}

for (auto IT = vtList.begin(); IT != vtList.end(); IT++){

…}

std::vector<std::tuple<std::string, int, double>> vtList;

Page 7: Welcome to Modern C++

7

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

// decltype(f)는 bool(const Widget&)

• 자세한 설명: http://devluna.blogspot.kr/2015/02/item-3-understand-decltype.html

Page 8: Welcome to Modern C++

8

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

authenticateUser();return c[i];

}

Page 9: Welcome to Modern C++
Page 10: Welcome to Modern C++

=default, =delete

initializer lists nullptr

for (x : coll)

override,

final

strongly-typed enumsenum class E {…};

aliases

Page 11: Welcome to Modern C++

11

int arr[] = { 1, 2, 3, 4, 5 };

for (int i = 0; i < 5; ++i)std::cout << arr[i] << std::endl;

int arr[] = { 1, 2, 3, 4, 5 };

for (auto& i : arr)std::cout << i << std::endl;

C++98 C++11

Page 12: Welcome to Modern C++

12

void f(int) void f(bool);

void f(void*);

f(0); // f(int)f(NULL); // f(int)f(nullptr); // f(void*)

• 자세한 설명: http://devluna.blogspot.kr/2015/04/item-08-0-null-nullptr-mva-version.html

Page 13: Welcome to Modern C++

13

template<typename FuncType, typename MuxType, typename PtrType>decltype(auto) lockAndCall (FuncType func, MuxType & mutex, PtrType 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

Page 14: Welcome to Modern C++

14

// C++98typedef std::unique_ptr<std::unordered_map<std::string, std::string>> UptrMapSS;

// C++11using UPtrMapSS = std::unique_ptr<std::unordered_map<std::string, std::string>>;

Page 15: Welcome to Modern C++

15

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;};

C++98template<typename T>using MyAllocList = std::list<T, MyAlloc<T>>;

MyAllocList<Widget> lw;

template<typename T>class Widget {private:

MyAllocList<T> list;};

C++11

• 자세한 설명: http://devluna.blogspot.kr/2015/03/item-9-typedef.html

Page 16: Welcome to Modern C++

16

enum Color { black, white, red, blue };

Color a = black;if (a < 14.5) { … } // OK

enum Feelings { excited, moody, blue }; // error

enum class Color { black, white, red, blue };

Color a = black; // errorColor b = Color::black; // OK if(b < 14.5) { … } // error

enum class Feelings { excited, moody, blue }; // OK

• 자세한 설명: http://devluna.blogspot.kr/2015/03/item-10-scoped-enum.html

Page 17: Welcome to Modern C++

17

class Widget {...private:

Widget(const Widget&);Widget& operator = (const Widget&);

}

Page 18: Welcome to Modern C++

18

class Widget {...

Widget(const Widget&) = delete;Widget& operator = (const Widget&) = delete;

}

bool isLucky(int number);

if (isLucky(‘a’)) ... // ‘a’(char) -> 97(int)if (isLucky(true)) ... // true(bool) -> 1(int)if (isLucky(3.5)) ... // 3.5(double) -> 3(int)

bool isLucky(int number);bool isLucky(char) = delete;

if (isLucky(‘a’)) ... // errorif (isLucky(true)) ... // true(bool) -> 1(int)if (isLucky(3.5)) ... // 3.5(double) -> 3(int)

• 자세한 설명: http://devluna.blogspot.kr/2015/03/item-11-private-delete.html

Page 19: Welcome to Modern C++

19 • 자세한 설명: http://devluna.blogspot.kr/2015/04/item-17-special.html

Page 20: Welcome to Modern C++

20

class Base {public:

virtual void mf1() const;virtual void mf2(int x);virtual void mf3() &;void mf4() const;

};

class Derived : public Base {public:

virtual void mf1();virtual void mf2(unsigned int x);virtual void mf3() &&;void mf4() const;

};

Page 21: Welcome to Modern C++

21

class Base {public:

virtual void mf1() const;virtual void mf2(int x);virtual void mf3() &;virtual void mf4() const;

};

class Derived : public Base {public:

virtual void mf1() override;virtual void mf2(unsigned int x) override;virtual void mf3() && override;void mf4() const override;

};

class Derived : public Base {public:

virtual void mf1() const override;virtual void mf2(int x) override;virtual void mf3() & override;void mf4() const override;

};

• 자세한 설명: http://devluna.blogspot.kr/2015/04/item-11-override.html

Page 22: Welcome to Modern C++

22

Object 초기화할때아래와같이할수있다.

int x(0); // initializer is in parentheses

int y = 0; // initializer follows "="

int z{0}; // initializer is in braces

int z = { 0 }; // initializer is in braces

Page 23: Welcome to Modern C++

23

non-static value에대한

default 초기값을설정하는데

( )는안된다.

class Widget

{

private:

int x{0}; // fine. x's default value is 0

int y = 0; // also fine

int z(0); // error!

};

std::atomic<int> ai1{0}; // fine

std::atomic<int> ai2(0); // fine

std::atomic<int> ai3 = 0; // error!

copy가안되는 object에대해서는

()는되는데, =는안된다.

둘다가능한건 { } 뿐이다.

- 모든상황에다사용이가능하다.

+ 기존에불가능했던것을쉽게사용할수있게해주었다.

std::vector<int> v{ 1, 3, 5 }; // v's initial content is 1, 3, 5

• 자세한 설명: http://devluna.blogspot.kr/2015/04/item-07-uniform-initializer.html

Page 24: Welcome to Modern C++

24

{ }를이용한생성자는가능한무조건 std::initializer_list 생성자를호출한다.

(더적합한생성자가있음에도불구하고…)

class Widget

{

public:

Widget(int i, bool b);

Widget(int i, double d);

Widget(std::initializer_list<long double> il);

...

};

Widget w2{ 10, true }; // 10 and true convert to long double

Widget w4{ 10, 5.0 }; // 10 and 5.0 convert to long double

Page 25: Welcome to Modern C++

25

Narrowing conversion 방지

class Widget {

public:

Widget(std::initializer_list<bool> il);

...

};

Widget w{10, 5.0}; // error! invalid narrowing conversion from 'double' to 'bool'

Page 26: Welcome to Modern C++

26

Most vexing parse 방지

class Widget {

public:

Widget();

Widget(std::initializer_list<int> il);

...

};

Widget w1; // calls default ctor

Widget w2{}; // also calls default ctor

Widget w3(); // most vexing parse! declares a function!

http://devluna.blogspot.kr/2015/01/item-6-c-most-vexing-parse.html