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
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
18. 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. 19 • 자세한 설명: http://devluna.blogspot.kr/2015/04/item-17-special.html
20. 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. 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. 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 방지
class Widget {
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