50shadesofc++...ˇ ˆ ˇ ˙˙˝˙˛ c++ meetingc++2018 50shadesofc++ nicolaijosuttis @nicojosuttis...
TRANSCRIPT
����������� �� ������ �� ���
������ �� ������������������
������������
�����
�� ������ �� ���
C++
MeetingC++�2018
50�Shades�of�C++
Nicolai�Josuttis@NicoJosuttis
����������� �� ������ �� ���
� � �!��� �����
class Customer {private:std::string name;
public:Customer (const std::string& n) : name(n) {assert(n.size() > 0);
}void setName(const std::string& n) {assert(n.size() > 0);name = n;
}std::string& getName() {return name;
}const std::string& getName() const {return name;
}};
std::ostream& operator<< (std::ostream& os, const Customer& c) {return os << c.getName();
}
������� �������������������
Customer c("Nico");c.setName(c.getName() + "lai");std::cout << "c: " << c << '\n';
"��#��$c: Nicolai
Nicolai Josuttis 50 Shades of C++
@NicoJosuttis 1
����������� �� ������ �� ���
Initialization
����������� �� ������ �� ���
%&��� '&������(����&
int i1; �� �&����&�� !����int i2 = 42; �� &���$ �&������(�� )��� *+int i3(42); �� �&������(�� )��� *+int i4 = int(); �� �&������(�� )��� �int i5{42}; �� �&������(�� )��� *+int i7{}; �� �&������(�� )��� �int i6 = {42}; �� �&������(�� )��� *+int i8 = {}; �� �&������(�� )��� �
int vals1[] = { 1, 2, 3 }; �� �&������(����& �� �,, �,����int vals2[] { 1, 2, 3 }; �� �&������(����& �� �,, �,����
std::complex<double> c1(4.0,3.0); �� �&������(����& �� ������std::complex<double> c2{4.0,3.0}; �� �&������(����& �� ������std::complex<double> c3 = {4.0,3.0}; �� �&������(����& �� ������
std::vector<std::string> cities{"Berlin", "Rome"};std::vector<int> vals2{0, 8, 15, i1+i2};std::vector<int> vals2 = {i1, i2, 42};
Nicolai Josuttis 50 Shades of C++
@NicoJosuttis 2
����������� �� ������ �� ���
-�) �� '&������(� �& int.
int i1;int i2 = 42; int i3(42);int i4 = int();int i5{42};int i7{};int i6 = {42};int i8 = {};auto i9 = 42;auto i10{42};auto i11 = {42};auto i12 = int{42};int i13();int i14(7, 9);int i15 = (7, 9);int i16 = int(7, 9);auto i17(7, 9);auto i18 = (7, 9);auto i19 = int(7, 9);
�� �&����&�� !������ &���$ �&��� )��� *+�� �&��� )��� *+�� �&��� )��� ��� �&��� )��� *+�� �&��� )��� ��� �&��� )��� *+�� �&��� )��� ��� �&��� �&� )��� *+ �� ...�� ...�� �&��� �&� )��� *+ �� ���� �� � ��&���&�� �#���/��� � � �� "01 �&��� �&� )��� 2 3�� �#� ��� 4�� �#���/��� � � �� �#���/��� � � �� "01 �&��� �&� )��� 2 3�� �#� ��� 4�� �#���/��� � �
����������� �� ������ �� ���
-�) �� '&������(� �& int.
int i1;int i2 = 42; int i3(42);int i4 = int();int i5{42};int i7{};int i6 = {42};int i8 = {};auto i9 = 42;auto i10{42};auto i11 = {42};auto i12 = int{42};int i13();int i14(7, 9);int i15 = (7, 9);int i16 = int(7, 9);auto i17(7, 9);auto i18 = (7, 9);auto i19 = int(7, 9);
�� �&����&�� !������ &���$ �&��� )��� *+�� �&��� )��� *+�� �&��� )��� ��� �&��� )��� *+�� �&��� )��� ��� �&��� )��� *+�� �&��� )��� ��� �&��� �&� )��� *+ �� ...�� ...�� �&��� �&� )��� *+ �� ���� �� � ��&���&�� �#���/��� � � �� "01 �&��� �&� )��� 2 3�� �#� ��� 4�� �#���/��� � � �� �#���/��� � � �� "01 �&��� �&� )��� 2 3�� �#� ��� 4�� �#���/��� � �
��&5� ��� ( ) �& �&������(����&�
Nicolai Josuttis 50 Shades of C++
@NicoJosuttis 3
����������� �� ������ �� ���
6����&, )��� auto
int i1;int i2 = 42; int i3(42);int i4 = int();int i5{42};int i7{};int i6 = {42};int i8 = {};
auto i9 = 42;auto i10{42};
�� �&����&�� !������ &���$ �&��� )��� *+�� �&��� )��� *+�� �&��� )��� ��� �&��� )��� *+�� �&��� )��� ��� �&��� )��� *+�� �&��� )��� �
�� �&��� �&� )��� *+ �� �����$ std::initializer_list<int>
������������������������������ � ��!��"#� !����� $���� �%�����&��
#���������� �� ������ �� ���
6����&, )��� auto
int i1;int i2 = 42; int i3(42);int i4 = int();int i5{42};int i7{};int i6 = {42};int i8 = {};
auto i9 = 42;auto i10{42};
�� �&����&�� !������ &���$ �&��� )��� *+�� �&��� )��� *+�� �&��� )��� ��� �&��� )��� *+�� �&��� )��� ��� �&��� )��� *+�� �&��� )��� �
�� �&��� �&� )��� *+ �� �&��� �&� )��� *+ 3�� ����' �'���(� � 4
������������������������������ � ��!��"#� !����� $���� �%�����&��
Nicolai Josuttis 50 Shades of C++
@NicoJosuttis 4
)���������� �� ������ �� ���
6����&, )��� auto
int i1;int i2 = 42; int i3(42);int i4 = int();int i5{42};int i7{};int i6 = {42};int i8 = {};
auto i9 = 42;auto i10{42};auto i11 = {42};
�� �&����&�� !������ &���$ �&��� )��� *+�� �&��� )��� *+�� �&��� )��� ��� �&��� )��� *+�� �&��� )��� ��� �&��� )��� *+�� �&��� )��� �
�� �&��� �&� )��� *+ �� �&��� �&� )��� *+ 3�� ����' �'���(� � 4�� ����� �&��� std::initializer_list<int> )��� *+
�& = �& �&������(����&�,�� ��&,� ��� �7#�
�&���������� �� ������ �� ���
6����&, )��� auto
int i1;int i2 = 42; int i3(42);int i4 = int();int i5{42};int i7{};int i6 = {42};int i8 = {};
auto i9 = 42;auto i10{42};auto i11 = {42};
std::initializer_list<auto> l = {42}; �� "0 ��&� ���+8 ...std::initializer_list l = {42}; �� "0 ��&� ���+8 ...
�� �&����&�� !������ &���$ �&��� )��� *+�� �&��� )��� *+�� �&��� )��� ��� �&��� )��� *+�� �&��� )��� ��� �&��� )��� *+�� �&��� )��� �
�� �&��� �&� )��� *+ �� �&��� �&� )��� *+ 3�� ����' �'���(� � 4�� ����� �&��� std::initializer_list<int> )��� *+
��&5� ��� = �& �&������(����&�
Nicolai Josuttis 50 Shades of C++
@NicoJosuttis 5
������������ �� ������ �� ���
����� ��)�7� ����
int i = 42;long v = 42;Customer c{"Jim", 77};vector<int>::const_iterator p
= v.begin();
int i = {42};
std::string x = "42";
std::atomic<int> a{9};std::array<int,5> r{};
long long ll{getInt()};
const C& r = f();
auto i = 42;auto v = 42l;auto c = Customer{"Jim", 77};auto p = v.cbegin();
auto i = {42}; �� initializer_list<int>auto i = int{42}; �� "0
using namespace std::literals;auto x = "42"s; �� ��&� ����*
auto a = std::atomic<int>{9}; �� "0 ��&� ����9auto r = std::array{}; �� ���� ��&� ����9
auto ll = long long{getInt()}; �� :;;";auto ll = int64_t(getInt()); �� ����� �&�auto ll = static_cast<long long>(getInt());
auto r = static_const<const C&>(f()); �� �"auto& r = static_const<const C&>(f()); �� "0auto&& r = static_const<const C&>(f()); �� "0
������������ �� ������ �� ���
���� 3����� ��)�7� ����<<4
int i = 42;long v = 42;Customer c{"Jim", 77};vector<int>::const_iterator p
= v.begin();
int i = {42};
std::string x = "42";
std::atomic<int> a{9};std::array<int,5> r{};
long long ll{getInt()};
const C& r = f();
auto i = 42;auto v = 42l;auto c = Customer{"Jim", 77};auto p = v.cbegin();
auto i = {42}; �� initializer_list<int>auto i = int{42}; �� "0
using namespace std::literals;auto x = "42"s; �� ��&� ����*
auto a = std::atomic<int>{9}; �� "0 ��&� ����9auto r = std::array{}; �� ���� ��&� ����9
auto ll = long long{getInt()}; �� :;;";auto ll = int64_t(getInt()); �� ����� �&�auto ll = static_cast<long long>(getInt());
auto r = static_const<const C&>(f()); �� �"auto& r = static_const<const C&>(f()); �� "0auto&& r = static_const<const C&>(f()); �� "0
Nicolai Josuttis 50 Shades of C++
@NicoJosuttis 6
������������ �� ������ �� ���
��� ��) =�7 �� '&������(����&
int i{42};
std::array<int,4> arr{0, i, i+10, i+20};
for (auto pos{arr.cbegin()}; pos < arr.cend(); pos += 2) {std::cout << *pos << '\n';
}
class Derived : Base {public:
Derived(int val) : Base{val} {}
};
for (int i{0}; i < 32; ++i) {std::cout << i << '\n';
}
>7 �������1 # ��� �� �� �&��� �&������(����&
� ���* �����)��� {+}� �� ���������)������ =
������������ �� ������ �� ���
� � �!��� �����$ '&������(����&�
class Customer {private:std::string name;
public:Customer (const std::string& n) : name{n} {assert(n.size() > 0);
}void setName(const std::string& n) {assert(n.size() > 0);name = n;
}std::string& getName() {return name;
}const std::string& getName() const {return name;
}};
std::ostream& operator<< (std::ostream& os, const Customer& c) {return os << c.getName();
}
Customer c{"Nico"};c.setName(c.getName() + "lai");std::cout << "c: " << c << '\n';
"��#��$c: Nicolai
Nicolai Josuttis 50 Shades of C++
@NicoJosuttis 7
������������ �� ������ �� ���
Move�Semantics
������������ �� ������ �� ���
� � �!��� �����$ ��&�� ��� �
class Customer {private:std::string name;
public:Customer (const std::string& n) : name{n} {assert(n.size() > 0);
}void setName(const std::string& n) {assert(n.size() > 0);name = n;
}std::string& getName() {return name;
}const std::string& getName() const {return name;
}};
std::ostream& operator<< (std::ostream& os, const Customer& c) {return os << c.getName();
}
Customer c{"Nico"};
"Nico"$ , � -&�
, � �
��������
�n$
-&
, � �
��������
�������
-&
Nicolai Josuttis 50 Shades of C++
@NicoJosuttis 8
������������ �� ������ �� ���
� � �!��� �����$ ��&�� ��� �
class Customer {private:std::string name;
public:Customer (std::string n) : name{std::move(n)} {assert(n.size() > 0);
}void setName(const std::string& n) {assert(n.size() > 0);name = n;
}std::string& getName() {return name;
}const std::string& getName() const {return name;
}};
std::ostream& operator<< (std::ostream& os, const Customer& c) {return os << c.getName();
}
Customer c{"Nico"};
"Nico"$ , � -&�
, � �
��������
�n$
-&
�#���������� �� ������ �� ���
� � �!��� �����$ ��&�� ��� �
class Customer {private:std::string name;
public:Customer (std::string n) : name{std::move(n)} {assert(n.size() > 0);
}void setName(const std::string& n) {assert(n.size() > 0);name = n;
}std::string& getName() {return name;
}const std::string& getName() const {return name;
}};
std::ostream& operator<< (std::ostream& os, const Customer& c) {return os << c.getName();
}
Customer c{"Nico"};
"Nico"$ , � -&�
��������
�������
, � �
���������(
�n$
-&
Nicolai Josuttis 50 Shades of C++
@NicoJosuttis 9
�)���������� �� ������ �� ���
� � �!��� �����$ ����� �
class Customer {private:std::string name;
public:Customer (std::string n) : name{std::move(n)} {assert(n.size() > 0);
}void setName(std::string n) {assert(n.size() > 0);name = std::move(n);
}std::string& getName() {return name;
}const std::string& getName() const {return name;
}};
std::ostream& operator<< (std::ostream& os, const Customer& c) {return os << c.getName();
}
?��� ��&@ #� ���� �. �� �&������(� � . �� ��� �A� �A7/!���� �&� �!�
�&���������� �� ������ �� ���
� � �!��� �����$ B���� �
class Customer {private:std::string name;
public:Customer (std::string n) : name{std::move(n)} {assert(n.size() > 0);
}void setName(std::string n) {assert(n.size() > 0);name = std::move(n);
}std::string& getName() {return name;
}const std::string& getName() const {return name;
}};
std::ostream& operator<< (std::ostream& os, const Customer& c) {return os << c.getName();
}
Customer c{"Nico"};c.getName() = "";
/ ��0������(�� ���*���� �� ����1� �
Nicolai Josuttis 50 Shades of C++
@NicoJosuttis 10
������������ �� ������ �� ���
� � �!��� �����$ B���� �
class Customer {private:std::string name;
public:Customer (std::string n) : name{std::move(n)} {assert(n.size() > 0);
}void setName(std::string n) {assert(n.size() > 0);name = std::move(n);
}
const std::string& getName() const {return name;
}};
std::ostream& operator<< (std::ostream& os, const Customer& c) {return os << c.getName();
}
Customer c{"Nico"};c.getName() = ""; �� "01 :;;";
������������ �� ������ �� ���
� � �!��� �����$ B���� �
class Customer {private:std::string name;
public:Customer (std::string n) : name{std::move(n)} {assert(n.size() > 0);
}void setName(std::string n) {assert(n.size() > 0);name = std::move(n);
}
const std::string& getName() const {return name;
}};
std::ostream& operator<< (std::ostream& os, const Customer& c) {return os << c.getName();
}
Customer crC(���); 22� �� �����������
�� ���# �!� �� � �� &�� �� � ��#� � 7$for (char c : crC(p).getName()) {cout << c; �� �&/��� :;;";
}
Nicolai Josuttis 50 Shades of C++
@NicoJosuttis 11
������������ �� ������ �� ���
� � �!��� �����$ B���� �
class Customer {private:std::string name;
public:Customer (std::string n) : name{std::move(n)} {assert(n.size() > 0);
}void setName(std::string n) {assert(n.size() > 0);name = std::move(n);
}
const std::string& getName() const {return name;
}};
std::ostream& operator<< (std::ostream& os, const Customer& c) {return os << c.getName();
}
;��� �&�� �C��&� ������� �� ��� &�� ��#� � ���$const auto& tmp1 = crC(p); �� ���� �C��&� ������� �� ��� & !����const auto& tmp2 = tmp1.getName(); �� "0std::cout << tmp2 << '\n'; �� "0
A�� &�� �� �&�� ���7 ��� �&�� ��#� � ���$const auto& tmp = crC(p).getName(); �� ���� &�� �C��&� ������� �� ��� & !���� �� crC()std::cout << tmp << '\n'; �� :;;";
;�&,�/A���� for ���#$auto&& _rg = crC(p).getName(); �� ���� &�� �C��&� ������� �� ��� & !���� �� crC()for (auto _pos=_rg.begin(), _end=_rg.end(); _pos!=_end; ++_pos ) {char c = *_pos;cout << c;
}
Customer crC(���); 22� �� �����������
�� ���# �!� �� � �� &�� �� � ��#� � 7$for (char c : crC(p).getName()) {cout << c; �� �&/��� :;;";
}
������������ �� ������ �� ���
� � �!��� �����$ B���� �
class Customer {private:std::string name;
public:Customer (std::string n) : name{std::move(n)} {assert(n.size() > 0);
}void setName(std::string n) {assert(n.size() > 0);name = std::move(n);
}std::string getName() && {return name;
}const std::string& getName() const& {return name;
}};
std::ostream& operator<< (std::ostream& os, const Customer& c) {return os << c.getName();
}
6�&5� ��� & ��� �&�� �� �A� � �� ��#� � ���3# !�����4
Nicolai Josuttis 50 Shades of C++
@NicoJosuttis 12
������������ �� ������ �� ���
Hidden�Friends
������������ �� ������ �� ���
-����& D ��&��.
#include <cassert>#include <string>
using namespace std;
int main(){auto x = L"a//b";auto y = string("a/b");assert(x == y); �� �����
}
Nicolai Josuttis 50 Shades of C++
@NicoJosuttis 13
������������ �� ������ �� ���
-����& D ��&��.
#include <cassert>#include <string>#include <filesystem>
using namespace std;using namespace std::filesystem;
int main(){auto x = L"a//b";auto y = string("a/b");assert(x == y); �� #�����
}
namespace std::filesystem {class path {public:
���path(string&& source);template<class Source>
path(const Source& source);���
};
bool operator==(const path& l,const path& r) noexcept;
���}
�#���������� �� ������ �� ���
-����& D ��&��.
#include <cassert>#include <string>#include <filesystem>
using namespace std;using namespace std::filesystem;
int main(){auto x = L"a//b";auto y = string("a/b");assert(x == y); �� "01 �����
}
namespace std::filesystem {class path {public:
���path(string&& source);template<class Source>
path(const Source& source);���friend bool
operator==(const path& l,const path& r) noexcept;
���};
}
Nicolai Josuttis 50 Shades of C++
@NicoJosuttis 14
�)���������� �� ������ �� ���
� � �!��� �����
class Customer {private:std::string name;
public:Customer (std::string n) : name{std::move(n)} {assert(n.size() > 0);
}void setName(std::string n) {assert(n.size() > 0);name = std::move(n);
}std::string getName() && {return name;
}const std::string& getName() const& {return name;
}
friend std::ostream& operator<< (std::ostream& os, const Customer& c) {return os << c.getName();
}};
: � ����,�� ���� 8� �&����� �� 8� �&������� �� �#� ��� EE
�&���������� �� ������ �� ���
Shades
Nicolai Josuttis 50 Shades of C++
@NicoJosuttis 15
������������ �� ������ �� ���
D���� �� ���&,� ��7��
class Customer {private:std::string name;
public:Customer (const std::string& n) : name(n) {assert(n.size() > 0);
}void setName(const std::string& n) {assert(n.size() > 0);name = n;
}std::string& getName() {return name;
}const std::string& getName() const {return name;
}};
std::ostream& operator<< (std::ostream& os, const Customer& c) {return os << c.getName();
}
class Customer {private:std::string name;
public:Customer (std::string n) : name{std::move(nassert(n.size() > 0);
}void setName(std::string n) {assert(n.size() > 0);name = std::move(n);
}std::string getName() && {return name;
}const std::string& getName() const& {return name;
}
friend std::ostream& operator<< (std::ostreareturn os << c.getName();
}};
;��@�� ��7��:C#� ��&�� ��7��
������������ �� ������ �� ���
;���� ���&,� ��7��
class Customer {private:std::string name;
public:Customer (const std::string& n) : name(n) {assert(n.size() > 0);
}void setName(const std::string& n) {assert(n.size() > 0);name = n;
}std::string& getName() {return name;
}const std::string& getName() const {return name;
}};
std::ostream& operator<< (std::ostream& os, const Customer& c) {return os << c.getName();
}
class Customer {private:std::string name;
public:Customer (std::string n) : name{std::move(nassert(n.size() > 0);
}void setName(std::string n) {assert(n.size() > 0);name = std::move(n);
}std::string getName() && {return name;
}const std::string& getName() const& {return name;
}
friend std::ostream& operator<< (std::ostreareturn os << c.getName();
}};
class Customer {private:std::string name;
public:Customer (const std::string& nassert(n.size() > 0);
}Customer (std::string&& n) : naassert(n.size() > 0);
}Customer (const char* n) : nameassert(n.size() > 0);
}void setName(const std::stringassert(n.size() > 0);name = n;
}void setName(std::string&& n) assert(n.size() > 0);name = std::move(n);
}void setName(const char* n) {
;��@�� ��7��:C#� ��&�� ��7��
D��&�����& # �, �� ��7��
Nicolai Josuttis 50 Shades of C++
@NicoJosuttis 16
������������ �� ������ �� ���
�� ������ �� ���
��� � �� &� ��&,�� )�7 �����&, ���
��� � �� &� ��&,�� ��� ��7��
������������ �� ������ �� ���
Why?
Nicolai Josuttis 50 Shades of C++
@NicoJosuttis 17
������������ �� ������ �� ���
���
� � # ���� ���� ��� ��� F�� 7�� � �,�
� ��&���&��7 �!��!�&,
� >�@)� � �#���A����7 �� @�73 �45�6��/5
� ���&��7 � �!�&
������������ �� ������ �� ���
D� �� ��� ���&�� ��(����& " ,�&�(����&
5%7258��9:����;5:<
%=�)��������������
;�<>5%��(( ��
�>�6�4>:%��(( ��
5�� �� ��(( ��
? ���!�6���������@
>���!��6�:� !��;5%2:%<
>�����%(���*��5�����!����6�5���1���
5������
%�����;4 ! "�A��!"<
% ����1@�B� 1�%�� ���2�&�#�����(� ������
Nicolai Josuttis 50 Shades of C++
@NicoJosuttis 18
������������ �� ������ �� ���
���&��7 6 �!�&
� ���&��7/6 �!�& ����)� �3 ,�����*�� �����3 ?��� ��� �
It's your fault, Andreiwhen "if constexpr" isnot the way you want it !You didn't come topropose it.
�#���������� �� ������ �� ���% ����1@�B� 1�%�� ���2�&�#�����(� ������
���)#���((��
��������((��
��������((��
��������((��
B �)�� �� ��� ���&�� ��(����&
% ����1@�B� 1�%�� ���2�&�#�����(� ������
Nicolai Josuttis 50 Shades of C++
@NicoJosuttis 19
�)���������� �� ������ �� ���
G� ,�&�� ���� ���&,� �����
��� ������ H��
&�) ������ H��
# ���� �&� �&��, ����&
����
�� ��,&����&�
� �&��� �&,�����
4� *
���
��
:���
�(�22���"��� � 0��(�"��2*� ��2��� ����!���� "(�*
�&���������� �� ������ �� ���
�� ������ �� ���
���
��!��
auto
��1���
decltype
* ����( �����
����(�
�� ��������( ���
char8_t
<=>
���� ���@
���* ������� �C���
��!�.1�����*
���
�����(
Nicolai Josuttis 50 Shades of C++
@NicoJosuttis 20
������������ �� ������ �� ���
6� @�
������������ �� ������ �� ���
G���� ����,� ���
0<; �$int i;i = 42; �� "042 = i; �� :;;";
int* p = &i; �� "0 �� �!���� �&�7int* q = &42; �� :;;";
���' �$const int c = 0;c = 42; �� :;;"; IJ &� �!����const int* r = &c; �� "0 IJ �!����
�����$std::string s;std::move(s) = "hello";auto sp = &std::move(s);
i �� �!������7D��� �*.����������*��������!�����42 �� !������
� @� �!�.����.���� *��������!����
c �� �!����� �� �C�1 � �� ���
!����� ���.� @ �� ���;' �!� @'<
std::move(���) �� C!�����. ��1�� �0�� �� ��. ��1�� �0�� �� ��
���� �� !���� ���� �##�7 �� C!�����$. A�E�� ������ �� ����( �� ��. ���� �� �� �� �( �����1��
�� "0 IJ �!������ :;;"; IJ &� �!����
Nicolai Josuttis 50 Shades of C++
@NicoJosuttis 21
������������ �� ������ �� ���
G���� ����,� ��� ��&� �����
� KG����$ K����(�A�� !����3 $� ��1 �F��������1� F�*�����F�� ��!� �� � F� �� ���� �� ��� �*� ����3 ����1������� �*������*��������!������ @��*��E�����*��1 �
� ?;G����$ ?� � ;G���� ;* �� �G$� ��<3 � �A�� � ������(�� ��!� �� � ��;421 true1 nullptrF"""<FthisF� ��1��F� �� ������. �*� ����F� ��� �*���� �� ��� �;T(""")<
� LG����$ �L#� �&, !����3 G�� ���� �� ��� �*� �����;�"!"�1@����$$�!�34<F������ �� ��� �*� ����
��( �����
�� ��! �� ��
�!���� C!���� # !����
:!� 7���&, ���� ��� � &�� �&� �� �&, ���� ���
��#� � ��� �&� ���� ���� ���
G���� � � ���$$�!�34
������������ �� ������ �� ���
����9$� # !����� #� �� �&������(����&
3 ,���( � @�1H���@�� ,�!����� # ���� ������&�
� Materialization �� � ��#� � 7 �A���$3 ( �� ��..��� �� ���� ���
G���� ����,� ��� ��&� ����9��( �����
�� ��,�!����
�� �� ��� �� # !����temporary materialization conversion
Nicolai Josuttis 50 Shades of C++
@NicoJosuttis 22
������������ �� ������ �� ���
�7#� !� ��� G���� ����,� 7
void passRValueRef (std::string&&); 22���� � ���
std::string s; �� � �� �!����passRValueRef(s); �� :;;";$ �&&�� A�&� �!���� �� !���� ��� �&�passRValueRef("hello"); �� "0passRValueRef(std::string("hello")); �� "0 3�& #��� # !����4passRValueRef(std::move(s)); �� "0 3�& #��� C!����4
void passRValueRef (std::string&& s){
passRValueRef(s); �� :;;";$ �&&�� A�&� �!���� �� !���� ��� �&�passRValueRef(std::move(s)); �� "0 3�& #��� C!����4
}
s ���$. �7#� std::string&&. G���� ���,� 7 �!����
;� ��!� �� � �; �� ��<����� ��������� ��!�;( �� ��<<
�& �&�7 #��� !�����
move() ���� s A�@ �� ��� �7#�$static_cast<std::string&&>(s)
������������ �� ������ �� ���
%&�!� ��� ;��� �&��
template <typename T>void passRValueRef (T&& s);
std::string s; �� � �� �!����passRValueRef(s); �� "0const std::string cs; �� � �� �!����passRValueRef(cs); �� "0
void passRValueRef (std::string&& s);
std::string s; �� � �� �!����passRValueRef(s); �� :;;";$ �&&�� A�&� �!���� �� !���� ��� �&�const std::string cs; �� � �� �!����passRValueRef(cs); �� :;;";$ �&&�� A�&� �!���� �� !���� ��� �&�
� �& �&�7 #��� !������ s &�� �&��
� �&�!� ��� ��� �&�o M !���� ��� �&� ���� �� � forwarding referenceM
� s �& A� �&��
template <typename T>void passRValueRef (typename T::iterator&&);
std::string::iterator pos1; �� � �� �!����passRValueRef(pos1); �� ...const std::string::iterator pos2; �� � �� �!����passRValueRef(pos2); �� ...
Nicolai Josuttis 50 Shades of C++
@NicoJosuttis 23
������������ �� ������ �� ���
;������ �7&��C �� %&�!� ��� ;��� �&��
template <typename T>void passRValueRef (T&&& s);
void passRValueRef (std::string&& s);
"!� �����&, �7&��C )��� ����� �&� ���&��� ��� ��� �� �C#� ��� � &�,��� � �� �##������& # �, �� �
�#���������� �� ������ �� ���
�� M�&�� �&� #� M$
���,� B�7�
� B��,�� �� MC++ BondageM
constint constint const *
'9������*������� 0���'
Nicolai Josuttis 50 Shades of C++
@NicoJosuttis 24
�)���������� �� ������ �� ���
���� B�7�
const int *
int * const �
�
�&���������� �� ������ �� ���
B� �& �����
Nicolai Josuttis 50 Shades of C++
@NicoJosuttis 25
������������ �� ������ �� ���
D ���
������������ �� ������ �� ���
D ���
We want you,application programmerfor the standardizationof C++
%��I�@� �B� 1�%��
We need style guides
�(( �����4 ! ����
Nicolai Josuttis 50 Shades of C++
@NicoJosuttis 26
������������ �� ������ �� ���
=��& ���� �&� -� A B�!� 6���� �&� ��!��
void sink(std::unique_ptr<MyType> up) 22����0;<�!������ ���(�����({
"""}
std::unique_ptr<MyType> up(new MyType);"""sink(std::move(up)); 22�up ������� ���(sink(up); 22�: � 1��������(@��!�*
22�����������@(��*�up ����� ���
-� A5� ��7��� &� ��� �� �!�� ,�� �&��� �� �!�
void sink(std::unique_ptr<MyType>&& up) 22��(� �*� ���(������� !����{
"""}
std::unique_ptr<MyType> up(new MyType);"""sink(std::move(up)); 22�up might ������ ���(sink(up); 22�: � �������� � ����*����0;<
����5� ��7��� ��� �� (���1 @ �!�� &� ,�� �&��� �� �!�
������������ �� ������ �� ���
��&� �����&, ��7�� B�����$ �� � B������&��
� ���#$�����##�,����A�����##�� �B������&����##�� �B������&��N;��/����
:��+8$ ? ��� ��� OP �&������(� �7&��C
3 ;����&� :��� � ���* �{} ����� �C����� �����( � F�� ��!��� � F� ����
��1�!���F�������*� ����* ��� �* ���*������ �C���"
3 ����� 7 ����1��������� �F������� � ������� ����(( @�������� @F���(���� @�
����� ��� �������@���������� ��J����������"
Nicolai Josuttis 50 Shades of C++
@NicoJosuttis 27
������������ �� ������ �� ���
��&� �����&, ��7�� B�����$ �A���� ��+
� ���#�$���A����������#����NA���/# �����/�� /�&������(����&"""3 >� ������* ������� �C������������ ����!�K
�" L���* �M������� ������� ��� ����������� ����1�!��@��� �������;* ��������� � ���� F��������(� � <�����������1���!��� ��������"
�" �����@������������� @�������������� ������ ��!��!�������������!� �0���"
"""3 :�����( ���N��������������������� ���������!��� �
��1������� ��!��!������ ������!���0��������!��*��������!�O�
3 � ����* ������� �C�����@���F������P�1� ��������!��� � �������1���*�������!������ ��1��0�"
�� �� 2�+���
������������ �� ������ �� ���
��&� �����&, ��7�� B�����$ �A���� +�+
� ���#�$���A����������#����NA���/# �����/�� /�&������(����&3 Q�������!������@���������
� ����� �C��!��� �� @3 ��������������� �� � ��� ���;* �����( �����F�* �F� ������ ��!��� ���<F3 ������� �(��� ����������������� ��R( F��������N��R( F3 ���������� ��;������� F�������(F���<F
� �����(� * ���!�� �������� �C���F� � ���!��(@���� ����"
3 Q������ ������ ���� �� ��@����;����(� �������<�������������� �C�������(� * ���!����������� !��F� ��� ��������( @���(���!��� ����!��� "
3 Q���ST������ �C������������J�� @��*�����1���(������P���(� �"
3 ,��� �����ST��������"
�� �� 2�+���
Nicolai Josuttis 50 Shades of C++
@NicoJosuttis 28
������������ �� ������ �� ���
� -�����&1 )� ��!� � # �A��3 ��( H�������� ���U�&�@�� ���!3 ����� @��� ���!3 /��0�� ����(��1� �@����0�@
� �45�6��/5
� 6 �!�& A7 ��� ��&��73 *���(� �
� ?�����1 ��@� � �3 %@ ��=�����
���
�#���������� �� ������ �� ���
������ �� ������������������
������������
�����
�� ������ �� ���
C++
Nicolai�Josuttis
Thank�youand�take�care
@NicoJosuttisjosuttis.com
V��� ����W 0���X
Nicolai Josuttis 50 Shades of C++
@NicoJosuttis 29