WareValley
http://www.warevalley.com
Overview
vector<vector<int>>
user-defined
literals thread_local
=default, =delete
atomic<T> auto f() -> int
array<T, N>
decltype
vector<LocalType>
noexcept
regex
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 templates
template <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
…
auto f() -> int
decltype
auto i = v.begin();
decltype(auto)
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
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
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;
7
int i = 10;
decltype(i) j = 10; // int
const int i2 = 0; // decltype(i2) 는 const int
bool f(const Widget& w); // decltype(w)는 const Widget&
// decltype(f)는 bool(const Widget&)
• 자세한 설명: http://devluna.blogspot.kr/2015/02/item-3-understand-decltype.html
8
template<typename Container, typename Index>
decltype(auto) authAndAccess(Container& c, Index i)
{
authenticateUser();
return c[i];
}
=default, =delete
initializer lists nullptr
for (x : coll)
override,
final
strongly-typed enums
enum class E {…};
aliases
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
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
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
14
// C++98
typedef std::unique_ptr<std::unordered_map<std::string, std::string>> UptrMapSS;
// C++11
using UPtrMapSS = std::unique_ptr<std::unordered_map<std::string, std::string>>;
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++98
template<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
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; // error
Color 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
17
class Widget {
...
private:
Widget(const Widget&);
Widget& operator = (const Widget&);
}
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’)) ... // error
if (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
19 • 자세한 설명: http://devluna.blogspot.kr/2015/04/item-17-special.html
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;
};
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
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
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
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
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'
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

Welcome to Modern C++

  • 1.
  • 2.
    Overview vector<vector<int>> user-defined literals thread_local =default, =delete atomic<T>auto f() -> int array<T, N> decltype vector<LocalType> noexcept regex 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 templates template <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 …
  • 3.
    auto f() ->int decltype auto i = v.begin(); decltype(auto)
  • 4.
    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
  • 5.
    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
  • 6.
    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;
  • 7.
    7 int i =10; decltype(i) j = 10; // int const int i2 = 0; // decltype(i2) 는 const int bool f(const Widget& w); // decltype(w)는 const Widget& // decltype(f)는 bool(const Widget&) • 자세한 설명: http://devluna.blogspot.kr/2015/02/item-3-understand-decltype.html
  • 8.
    8 template<typename Container, typenameIndex> decltype(auto) authAndAccess(Container& c, Index i) { authenticateUser(); return c[i]; }
  • 10.
    =default, =delete initializer listsnullptr for (x : coll) override, final strongly-typed enums enum class E {…}; aliases
  • 11.
    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
  • 12.
    12 void f(int) void f(bool); voidf(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
  • 13.
    13 template<typename FuncType, typenameMuxType, 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
  • 14.
    14 // C++98 typedef std::unique_ptr<std::unordered_map<std::string,std::string>> UptrMapSS; // C++11 using UPtrMapSS = std::unique_ptr<std::unordered_map<std::string, std::string>>;
  • 15.
    15 template<typename T> struct MyAlloList{ typedefstd::list<T,myalloc<T>> type; }; MyAllocList<Widget>::type lw; template<typename T> class Widget { private: typename MyAllocList<T>::type list; }; C++98 template<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
  • 16.
    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; // error Color 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
  • 17.
    17 class Widget { ... private: Widget(constWidget&); Widget& operator = (const Widget&); }
  • 18.
    18 class Widget { ... Widget(constWidget&) = 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’)) ... // error if (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
  • 19.
    19 • 자세한설명: http://devluna.blogspot.kr/2015/04/item-17-special.html
  • 20.
    20 class Base { public: virtualvoid 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; };
  • 21.
    21 class Base { public: virtualvoid 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
  • 22.
    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
  • 23.
    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
  • 24.
    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
  • 25.
    25 Narrowing conversion 방지 classWidget { public: Widget(std::initializer_list<bool> il); ... }; Widget w{10, 5.0}; // error! invalid narrowing conversion from 'double' to 'bool'
  • 26.
    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