- the worst feature of modern c++: default behavior di… · the worst feature of modern c++:...

178
The worst feature of modern C++: default behavior Marcin Grzebieluch [email protected] 21.11.2017 grzebiel 21.11.2017 1 / 77

Upload: others

Post on 01-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

The worst feature of modern C++: default behavior

Marcin Grzebieluch

[email protected]

21.11.2017

grzebiel 21.11.2017 1 / 77

Page 2: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

The worst feature of modern C++: default behavior

Marcin Grzebieluch

[email protected]

21.11.2017

grzebiel 21.11.2017 2 / 77

Page 3: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

Questions

grzebiel 21.11.2017 3 / 77

Page 4: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

Plan

1 auto

2 decltype

3 default args

4 rvalue reference

5 syntax

grzebiel 21.11.2017 4 / 77

Page 5: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 auto

2 decltype

3 default args

4 rvalue reference

5 syntax

auto grzebiel 21.11.2017 5 / 77

Page 6: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

first use of c++11 auto1 #include <cassert>2 int& referenceToInt();34 void modifyFoo()5 {6 auto x = referenceToInt();7 x = 5;8 }9

10 int main()11 {12 modifyFoo();13 assert(referenceToInt() == 5);14 return 0;15 }

auto grzebiel 21.11.2017 6 / 77

Page 7: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

first use of c++11 auto1 #include <cassert>2 int& referenceToInt();34 void modifyFoo()5 {6 auto x = referenceToInt();7 x = 5;8 }9

10 int main()11 {12 modifyFoo();13 assert(referenceToInt() == 5);14 return 0;15 }

auto grzebiel 21.11.2017 6 / 77

Page 8: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

first use of c++11 auto1 #include <cassert>2 int& referenceToInt();34 void modifyFoo()5 {6 auto x = referenceToInt();7 x = 5;8 }9

10 int main()11 {12 modifyFoo();13 assert(referenceToInt() == 5);14 return 0;15 }

auto grzebiel 21.11.2017 6 / 77

Page 9: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

first use of c++11 auto1 #include <cassert>2 int& referenceToInt();34 void modifyFoo()5 {6 auto x = referenceToInt();7 x = 5;8 }9

10 int main()11 {12 modifyFoo();13 assert(referenceToInt() == 5);14 return 0;15 }

auto grzebiel 21.11.2017 6 / 77

Page 10: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

first use of c++11 auto1 #include <cassert>2 int& referenceToInt();34 void modifyFoo()5 {6 auto x = referenceToInt();7 x = 5;8 }9

10 int main()11 {12 modifyFoo();13 assert(referenceToInt() == 5);14 return 0;15 }

auto grzebiel 21.11.2017 6 / 77

Page 11: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

cpp11 standard:Once the type of a declarator-id has been determinedaccording to 8.3, the type of the declared variable using thedeclarator-id is determined from the type of itsinitializer using the rules for template argumentdeduction.

auto grzebiel 21.11.2017 7 / 77

Page 12: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

cpp11 standard:If template parameter type P is a reference type, the typereferred to by P is used for type deduction.

auto grzebiel 21.11.2017 8 / 77

Page 13: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

lets fix code1 #include <cassert>2 int& referenceToInt();34 void modifyFoo()5 {6 auto x = referenceToInt();7 x = 5;8 }9

10 int main()11 {12 modifyFoo();13 assert(referenceToInt() == 5);14 return 0;15 }

auto grzebiel 21.11.2017 9 / 77

Page 14: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

lets fix code1 #include <cassert>2 int& referenceToInt();34 void modifyFoo()5 {6 auto x = referenceToInt();7 x = 5;8 }9

10 int main()11 {12 modifyFoo();13 assert(referenceToInt() == 5);14 return 0;15 }

auto grzebiel 21.11.2017 9 / 77

Page 15: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

lets fix code1 #include <cassert>2 int& referenceToInt();34 void modifyFoo()5 {6 auto& x = referenceToInt();7 x = 5;8 }9

10 int main()11 {12 modifyFoo();13 assert(referenceToInt() == 5);14 return 0;15 }

auto grzebiel 21.11.2017 9 / 77

Page 16: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

what have I learned?

• before the usage, always read at leset 10 setencesabout the future

• auto does not deduce consts, references and volatiles

auto grzebiel 21.11.2017 10 / 77

Page 17: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

what have I learned?

• before the usage, always read at leset 10 setencesabout the future

• auto does not deduce consts, references and volatiles

auto grzebiel 21.11.2017 10 / 77

Page 18: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

what have I learned?

• before the usage, always read at leset 10 setencesabout the future

• auto does not deduce consts, references and volatiles

auto grzebiel 21.11.2017 10 / 77

Page 19: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

it was not only me!

auto grzebiel 21.11.2017 11 / 77

Page 20: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

it was not only me!

auto grzebiel 21.11.2017 11 / 77

Page 21: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

Lets imagine that we are in alternative universe in which:

1 int& foo();23 auto a = foo();4 // type of a would be int&

auto grzebiel 21.11.2017 12 / 77

Page 22: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

a magical place

https://en.wikipedia.org/wiki/File:Bliss.pngauto grzebiel 21.11.2017 13 / 77

Page 23: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

now it works1 #include <cassert>2 int& referenceToInt();34 void modifyFoo()5 {6 auto x = referenceToInt();7 x = 5;8 }9

10 int main()11 {12 modifyFoo();13 assert(referenceToInt() == 5);14 return 0;15 }

auto grzebiel 21.11.2017 14 / 77

Page 24: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

in this alternative world

1 int a = 5;2 int* b = &a;3 auto c = *b;4 ++c;5 assert(c != a);

auto grzebiel 21.11.2017 15 / 77

Page 25: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

in this alternative world

1 int a = 5;2 int* b = &a;3 auto c = *b;4 ++c;5 assert(c != a);

auto grzebiel 21.11.2017 15 / 77

Page 26: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

in this alternative world

1 int a = 5;2 int* b = &a;3 auto c = *b;4 ++c;5 assert(c != a);

auto grzebiel 21.11.2017 15 / 77

Page 27: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

in this alternative world

1 int a = 5;2 int* b = &a;3 auto c = *b;4 ++c;5 assert(c != a);

auto grzebiel 21.11.2017 15 / 77

Page 28: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

in this alternative world

1 int a = 5;2 int* b = &a;3 auto c = *b;4 ++c;5 assert(c != a);

auto grzebiel 21.11.2017 15 / 77

Page 29: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

in this alternative world

1 int a = 5;2 int* b = &a;3 auto c = *b;4 ++c;5 assert(c != a);

auto grzebiel 21.11.2017 15 / 77

Page 30: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

in this alternative world

1 auto a = 5;2 auto b = a;

type of variable bwould be int &

auto grzebiel 21.11.2017 16 / 77

Page 31: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

in this alternative world

1 auto a = 5;2 auto b = a;

type of variable bwould be int &

auto grzebiel 21.11.2017 16 / 77

Page 32: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

back to normal world

auto grzebiel 21.11.2017 17 / 77

Page 33: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

auto means template type deduction

auto grzebiel 21.11.2017 18 / 77

Page 34: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 auto foo() -> int;

auto grzebiel 21.11.2017 19 / 77

Page 35: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

auto means template type deduction

except when it’s not

auto grzebiel 21.11.2017 20 / 77

Page 36: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

auto means template type deductionexcept when it’s not

auto grzebiel 21.11.2017 20 / 77

Page 37: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 template <typename T, typename U>

2 auto add(T t, U u) -> decltype(t + u);

auto grzebiel 21.11.2017 21 / 77

Page 38: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 #include <utility>23 template<typename T, typename U>4 decltype(std::declval<T>() + std::declval<U>())5 add(T t, U u);

auto grzebiel 21.11.2017 22 / 77

Page 39: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 template <typename T, typename U>

2 auto add(T t, U u) -> decltype(t + u);

auto grzebiel 21.11.2017 23 / 77

Page 40: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 #include <memory>23 template <typename T>4 struct unique_ptr_wrapper5 {6 std::unique_ptr<T> uptr;7 //...8 auto operator*() -> decltype(*uptr)9 {

10 return *uptr;11 }12 //...13 };

auto grzebiel 21.11.2017 24 / 77

Page 41: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 #include <memory>23 template <typename T>4 struct unique_ptr_wrapper5 {6 std::unique_ptr<T> uptr;7 //...8 auto operator*() -> decltype(*uptr)9 {

10 return *uptr;11 }12 //...13 };

auto grzebiel 21.11.2017 24 / 77

Page 42: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 #include <memory>23 template <typename T>4 struct unique_ptr_wrapper5 {6 std::unique_ptr<T> uptr;7 //...8 auto operator*() -> decltype(*uptr)9 {

10 return *uptr;11 }12 //...13 };

auto grzebiel 21.11.2017 24 / 77

Page 43: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 #include <memory>23 template <typename T>4 struct unique_ptr_wrapper5 {6 std::unique_ptr<T> uptr;7 //...8 auto operator*() -> decltype(*uptr)9 {

10 return *uptr;11 }12 //...13 };

auto grzebiel 21.11.2017 24 / 77

Page 44: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 #include <memory>23 template <typename T>4 struct unique_ptr_wrapper5 {6 std::unique_ptr<T> uptr;7 //...8 auto operator*()9 {

10 return *uptr;11 }12 //...13 };

auto grzebiel 21.11.2017 25 / 77

Page 45: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 #include <memory>23 template <typename T>4 struct unique_ptr_wrapper5 {6 std::unique_ptr<T> uptr;7 //...8 auto operator*()9 {

10 return *uptr;11 }12 //...13 };

auto grzebiel 21.11.2017 25 / 77

Page 46: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 #include <memory>23 template <typename T>4 struct unique_ptr_wrapper5 {6 std::unique_ptr<T> uptr;7 //...8 auto& operator*()9 {

10 return *uptr;11 }12 //...13 };

auto grzebiel 21.11.2017 25 / 77

Page 47: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 #include <memory>23 template <typename T>4 struct unique_ptr_wrapper5 {6 std::unique_ptr<T> uptr;7 //...8 decltype(auto) operator*()9 {

10 return *uptr;11 }12 //...13 };

auto grzebiel 21.11.2017 26 / 77

Page 48: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 auto

2 decltype

3 default args

4 rvalue reference

5 syntax

decltype grzebiel 21.11.2017 27 / 77

Page 49: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

quiz time!

what is a type of variable y?

decltype grzebiel 21.11.2017 28 / 77

Page 50: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

quiz time!

1 int x;2 decltype(x) y;

int

decltype grzebiel 21.11.2017 29 / 77

Page 51: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

quiz time!

1 int x;2 decltype(x) y;

int

decltype grzebiel 21.11.2017 29 / 77

Page 52: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

quiz time!

1 int x;2 auto& z = x;3 decltype(z) y;

int &

decltype grzebiel 21.11.2017 30 / 77

Page 53: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

quiz time!

1 int x;2 auto& z = x;3 decltype(z) y;

int &

decltype grzebiel 21.11.2017 30 / 77

Page 54: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

quiz time!

1 int x;2 decltype(&x) y;

int *

decltype grzebiel 21.11.2017 31 / 77

Page 55: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

quiz time!

1 int x;2 decltype(&x) y;

int *

decltype grzebiel 21.11.2017 31 / 77

Page 56: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

quiz time!

1 int x;2 decltype(x + 3.2) y;

double

decltype grzebiel 21.11.2017 32 / 77

Page 57: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

quiz time!

1 int x;2 decltype(x + 3.2) y;

double

decltype grzebiel 21.11.2017 32 / 77

Page 58: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

quiz time!

1 struct S { int m; };23 S s{};4 decltype(s.m) y;

int

decltype grzebiel 21.11.2017 33 / 77

Page 59: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

quiz time!

1 struct S { int m; };23 S s{};4 decltype(s.m) y;

int

decltype grzebiel 21.11.2017 33 / 77

Page 60: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

quiz time!

1 int* x;2 decltype(*x) y;

int&

decltype grzebiel 21.11.2017 34 / 77

Page 61: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

quiz time!

1 int* x;2 decltype(*x) y;

int&

decltype grzebiel 21.11.2017 34 / 77

Page 62: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

quiz time!

1 int x;2 decltype((x)) y;

int&

decltype grzebiel 21.11.2017 35 / 77

Page 63: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

quiz time!

1 int x;2 decltype((x)) y;

int&

decltype grzebiel 21.11.2017 35 / 77

Page 64: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

quiz time!

1 struct S{int m;};23 const S* sptr;4 decltype((sptr->m)) y;

const int&

decltype grzebiel 21.11.2017 36 / 77

Page 65: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

quiz time!

1 struct S{int m;};23 const S* sptr;4 decltype((sptr->m)) y;

const int&

decltype grzebiel 21.11.2017 36 / 77

Page 66: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

quiz time!

1 auto string = "string literal";2 decltype(string) y;

const char*

decltype grzebiel 21.11.2017 37 / 77

Page 67: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

quiz time!

1 auto string = "string literal";2 decltype(string) y;

const char*

decltype grzebiel 21.11.2017 37 / 77

Page 68: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

quiz time!

1 decltype("string literal") y;

const char (&)[15]

decltype grzebiel 21.11.2017 38 / 77

Page 69: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

quiz time!

1 decltype("string literal") y;

const char (&)[15]

decltype grzebiel 21.11.2017 38 / 77

Page 70: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

decltype has two seperate usecases which yield differentresults:

• get type of entity

• get type of expression

decltype grzebiel 21.11.2017 39 / 77

Page 71: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

decltype has two seperate usecases which yield differentresults:

• get type of entity

• get type of expression

decltype grzebiel 21.11.2017 39 / 77

Page 72: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

decltype has two seperate usecases which yield differentresults:

• get type of entity

• get type of expression

decltype grzebiel 21.11.2017 39 / 77

Page 73: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 int x;2 decltype(x); // an entity3 decltype(5); // expression4 decltype((x)); // expression

decltype grzebiel 21.11.2017 40 / 77

Page 74: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 int x;2 decltype(x); // an entity3 decltype(5); // expression4 decltype((x)); // expression

decltype grzebiel 21.11.2017 40 / 77

Page 75: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 int x;2 decltype(x); // an entity3 decltype(5); // expression4 decltype((x)); // expression

decltype grzebiel 21.11.2017 40 / 77

Page 76: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 int x;2 decltype(x); // an entity3 decltype(5); // expression4 decltype((x)); // expression

decltype grzebiel 21.11.2017 40 / 77

Page 77: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 struct S{int m;};23 const S* sptr;4 decltype((sptr->m)) y;

decltype grzebiel 21.11.2017 41 / 77

Page 78: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 auto string = "string literal";2 decltype(string) y;

decltype grzebiel 21.11.2017 42 / 77

Page 79: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 decltype("string literal") y;

decltype grzebiel 21.11.2017 43 / 77

Page 80: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 auto

2 decltype

3 default args

4 rvalue reference

5 syntax

default args grzebiel 21.11.2017 44 / 77

Page 81: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

inheritance!

1 void say(std::string who,2 int what)3 {4 std::cout << who << " says: "5 << what << std::endl;6 }78 struct Parent9 {

10 virtual void say(int a = 5)11 {12 ::say("Parent", a);13 }1415 virtual ~Parent() = default;16 };

1718 class Child : public Parent19 {20 void say(int a = 7) override21 {22 ::say("Child", a);23 }24 };2526 int main() {27 Child c{};28 Parent* p = &c;2930 p->say();31 }

default args grzebiel 21.11.2017 45 / 77

Page 82: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

inheritance!

1 void say(std::string who,2 int what)3 {4 std::cout << who << " says: "5 << what << std::endl;6 }78 struct Parent9 {

10 virtual void say(int a = 5)11 {12 ::say("Parent", a);13 }1415 virtual ~Parent() = default;16 };

1718 class Child : public Parent19 {20 void say(int a = 7) override21 {22 ::say("Child", a);23 }24 };2526 int main() {27 Child c{};28 Parent* p = &c;2930 p->say();31 }

default args grzebiel 21.11.2017 45 / 77

Page 83: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

inheritance!

1 void say(std::string who,2 int what)3 {4 std::cout << who << " says: "5 << what << std::endl;6 }78 struct Parent9 {

10 virtual void say(int a = 5)11 {12 ::say("Parent", a);13 }1415 virtual ~Parent() = default;16 };

1718 class Child : public Parent19 {20 void say(int a = 7) override21 {22 ::say("Child", a);23 }24 };2526 int main() {27 Child c{};28 Parent* p = &c;2930 p->say();31 }

default args grzebiel 21.11.2017 45 / 77

Page 84: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

inheritance!

1 void say(std::string who,2 int what)3 {4 std::cout << who << " says: "5 << what << std::endl;6 }78 struct Parent9 {

10 virtual void say(int a = 5)11 {12 ::say("Parent", a);13 }1415 virtual ~Parent() = default;16 };

1718 class Child : public Parent19 {20 void say(int a = 7) override21 {22 ::say("Child", a);23 }24 };2526 int main() {27 Child c{};28 Parent* p = &c;2930 p->say();31 }

default args grzebiel 21.11.2017 45 / 77

Page 85: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

inheritance!

1 void say(std::string who,2 int what)3 {4 std::cout << who << " says: "5 << what << std::endl;6 }78 struct Parent9 {

10 virtual void say(int a = 5)11 {12 ::say("Parent", a);13 }1415 virtual ~Parent() = default;16 };

1718 class Child : public Parent19 {20 void say(int a = 7) override21 {22 ::say("Child", a);23 }24 };2526 int main() {27 Child c{};28 Parent* p = &c;2930 p->say();31 }

default args grzebiel 21.11.2017 45 / 77

Page 86: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

inheritance!

1 void say(std::string who,2 int what)3 {4 std::cout << who << " says: "5 << what << std::endl;6 }78 struct Parent9 {

10 virtual void say(int a = 5)11 {12 ::say("Parent", a);13 }1415 virtual ~Parent() = default;16 };

1718 class Child : public Parent19 {20 void say(int a = 7) override21 {22 ::say("Child", a);23 }24 };2526 int main() {27 Child c{};28 Parent* p = &c;2930 p->say();31 }

default args grzebiel 21.11.2017 45 / 77

Page 87: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

inheritance!

1 void say(std::string who,2 int what)3 {4 std::cout << who << " says: "5 << what << std::endl;6 }78 struct Parent9 {

10 virtual void say(int a = 5)11 {12 ::say("Parent", a);13 }1415 virtual ~Parent() = default;16 };

1718 class Child : public Parent19 {20 void say(int a = 7) override21 {22 ::say("Child", a);23 }24 };2526 int main() {27 Child c{};28 Parent* p = &c;2930 p->say();31 }

default args grzebiel 21.11.2017 45 / 77

Page 88: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

inheritance!

1 void say(std::string who,2 int what)3 {4 std::cout << who << " says: "5 << what << std::endl;6 }78 struct Parent9 {

10 virtual void say(int a = 5)11 {12 ::say("Parent", a);13 }1415 virtual ~Parent() = default;16 };

1718 class Child : public Parent19 {20 void say(int a = 7) override21 {22 ::say("Child", a);23 }24 };2526 int main() {27 Child c{};28 Parent* p = &c;2930 p->say();31 }

default args grzebiel 21.11.2017 45 / 77

Page 89: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

inheritance!

1 void say(std::string who,2 int what)3 {4 std::cout << who << " says: "5 << what << std::endl;6 }78 struct Parent9 {

10 virtual void say(int a = 5)11 {12 ::say("Parent", a);13 }1415 virtual ~Parent() = default;16 };

1718 class Child : public Parent19 {20 void say(int a = 7) override21 {22 ::say("Child", a);23 }24 };2526 int main() {27 Child c{};28 Parent* p = &c;2930 p->say();31 }

default args grzebiel 21.11.2017 45 / 77

Page 90: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

and the output is

Child says: 5

default args grzebiel 21.11.2017 46 / 77

Page 91: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

and the output is

Child says:

5

default args grzebiel 21.11.2017 46 / 77

Page 92: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

and the output is

Child says: 5

default args grzebiel 21.11.2017 46 / 77

Page 93: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

default args grzebiel 21.11.2017 47 / 77

Page 94: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

function call phases

default args grzebiel 21.11.2017 48 / 77

Page 95: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

function call phases

• name lookup

default args grzebiel 21.11.2017 48 / 77

Page 96: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

function call phases

• name lookup

• template type deduction

default args grzebiel 21.11.2017 48 / 77

Page 97: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

function call phases

• name lookup

• template type deduction

• overload resolution

default args grzebiel 21.11.2017 48 / 77

Page 98: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

function call phases

• name lookup

• template type deduction

• overload resolution

• access control

default args grzebiel 21.11.2017 48 / 77

Page 99: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

function call phases

• name lookup

• template type deduction

• overload resolution

• access control

• virtual function call

default args grzebiel 21.11.2017 48 / 77

Page 100: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

function call phases

• name lookup

• template type deduction

• overload resolution

• access control

• virtual function call

default args grzebiel 21.11.2017 48 / 77

Page 101: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

after finding default argument

1 void say(std::string who,2 int what)3 {4 std::cout << who << " says: "5 << what << std::endl;6 }78 struct Parent9 {

10 virtual void say(int a = 5)11 {12 ::say("Parent", a);13 }1415 virtual ~Parent() = default;16 };

1718 class Child : public Parent19 {20 void say(int a = 7) override21 {22 ::say("Child", a);23 }24 };2526 int main() {27 Child c{};28 Parent* p = &c;2930 p->say();31 }

default args grzebiel 21.11.2017 49 / 77

Page 102: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

after finding default argument

1 void say(std::string who,2 int what)3 {4 std::cout << who << " says: "5 << what << std::endl;6 }78 struct Parent9 {

10 virtual void say(int a)11 {12 ::say("Parent", a);13 }1415 virtual ~Parent() = default;16 };

1718 class Child : public Parent19 {20 void say(int a) override21 {22 ::say("Child", a);23 }24 };2526 int main() {27 Child c{};28 Parent* p = &c;2930 p->say(5);31 }

default args grzebiel 21.11.2017 49 / 77

Page 103: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

function call phases

• name lookup

• template type deduction

• overload resolution

• access control

• virtual function call

default args grzebiel 21.11.2017 50 / 77

Page 104: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

function call phases

• name lookup

• template type deduction

• overload resolution

• access control

• virtual function call

default args grzebiel 21.11.2017 50 / 77

Page 105: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

access control

1 void say(std::string who,2 int what)3 {4 std::cout << who << " says: "5 << what << std::endl;6 }78 struct Parent9 {

10 virtual void say(int a)11 {12 ::say("Parent", a);13 }1415 virtual ~Parent() = default;16 };

1718 class Child : public Parent19 {20 void say(int a) override21 {22 ::say("Child", a);23 }24 };2526 int main() {27 Child c{};28 Parent* p = &c;2930 p->say(5);31 }

default args grzebiel 21.11.2017 51 / 77

Page 106: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

access control

1 void say(std::string who,2 int what)3 {4 std::cout << who << " says: "5 << what << std::endl;6 }78 struct Parent9 {

10 virtual void say(int a)11 {12 ::say("Parent", a);13 }1415 virtual ~Parent() = default;16 };

1718 class Child : public Parent19 {20 void say(int a) override21 {22 ::say("Child", a);23 }24 };2526 int main() {27 Child c{};28 Parent* p = &c;2930 p->say(5);31 }

default args grzebiel 21.11.2017 51 / 77

Page 107: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

access control

1 void say(std::string who,2 int what)3 {4 std::cout << who << " says: "5 << what << std::endl;6 }78 struct Parent9 {

10 virtual void say(int a)11 {12 ::say("Parent", a);13 }1415 virtual ~Parent() = default;16 };

1718 class Child : public Parent19 {20 void say(int a) override21 {22 ::say("Child", a);23 }24 };2526 int main() {27 Child c{};28 Parent* p = &c;2930 p->say(5);31 }

default args grzebiel 21.11.2017 51 / 77

Page 108: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

access control

1 void say(std::string who,2 int what)3 {4 std::cout << who << " says: "5 << what << std::endl;6 }78 struct Parent9 {

10 virtual void say(int a)11 {12 ::say("Parent", a);13 }1415 virtual ~Parent() = default;16 };

1718 class Child : public Parent19 {20 void say(int a) override21 {22 ::say("Child", a);23 }24 };2526 int main() {27 Child c{};28 Parent* p = &c;2930 p->say(5);31 }

default args grzebiel 21.11.2017 51 / 77

Page 109: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

argument redeclaration1 int foo(int a, int b = 5);23 int main()4 {5 foo(1); // a = 1, b = 567 int foo(int a, int b = 7);8 foo(2); // a = 2, b = 7;9 {

10 int foo(int a = 3, int b = 12);11 foo(); // a = 3, b = 12;12 // ::foo(); // compilation error13 ::foo(4); // a = 4, b = 5;14 }15 }

default args grzebiel 21.11.2017 52 / 77

Page 110: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

argument redeclaration1 int foo(int a, int b = 5);23 int main()4 {5 foo(1); // a = 1, b = 567 int foo(int a, int b = 7);8 foo(2); // a = 2, b = 7;9 {

10 int foo(int a = 3, int b = 12);11 foo(); // a = 3, b = 12;12 // ::foo(); // compilation error13 ::foo(4); // a = 4, b = 5;14 }15 }

default args grzebiel 21.11.2017 52 / 77

Page 111: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

argument redeclaration1 int foo(int a, int b = 5);23 int main()4 {5 foo(1); // a = 1, b = 567 int foo(int a, int b = 7);8 foo(2); // a = 2, b = 7;9 {

10 int foo(int a = 3, int b = 12);11 foo(); // a = 3, b = 12;12 // ::foo(); // compilation error13 ::foo(4); // a = 4, b = 5;14 }15 }

default args grzebiel 21.11.2017 52 / 77

Page 112: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

argument redeclaration1 int foo(int a, int b = 5);23 int main()4 {5 foo(1); // a = 1, b = 567 int foo(int a, int b = 7);8 foo(2); // a = 2, b = 7;9 {

10 int foo(int a = 3, int b = 12);11 foo(); // a = 3, b = 12;12 // ::foo(); // compilation error13 ::foo(4); // a = 4, b = 5;14 }15 }

default args grzebiel 21.11.2017 52 / 77

Page 113: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

argument redeclaration1 int foo(int a, int b = 5);23 int main()4 {5 foo(1); // a = 1, b = 567 int foo(int a, int b = 7);8 foo(2); // a = 2, b = 7;9 {

10 int foo(int a = 3, int b = 12);11 foo(); // a = 3, b = 12;12 // ::foo(); // compilation error13 ::foo(4); // a = 4, b = 5;14 }15 }

default args grzebiel 21.11.2017 52 / 77

Page 114: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

argument redeclaration1 int foo(int a, int b = 5);23 int main()4 {5 foo(1); // a = 1, b = 567 int foo(int a, int b = 7);8 foo(2); // a = 2, b = 7;9 {

10 int foo(int a = 3, int b = 12);11 foo(); // a = 3, b = 12;12 // ::foo(); // compilation error13 ::foo(4); // a = 4, b = 5;14 }15 }

default args grzebiel 21.11.2017 52 / 77

Page 115: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

argument redeclaration1 int foo(int a, int b = 5);23 int main()4 {5 foo(1); // a = 1, b = 567 int foo(int a, int b = 7);8 foo(2); // a = 2, b = 7;9 {

10 int foo(int a = 3, int b = 12);11 foo(); // a = 3, b = 12;12 // ::foo(); // compilation error13 ::foo(4); // a = 4, b = 5;14 }15 }

default args grzebiel 21.11.2017 52 / 77

Page 116: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

argument redeclaration1 int foo(int a, int b = 5);23 int main()4 {5 foo(1); // a = 1, b = 567 int foo(int a, int b = 7);8 foo(2); // a = 2, b = 7;9 {

10 int foo(int a = 3, int b = 12);11 foo(); // a = 3, b = 12;12 // ::foo(); // compilation error13 ::foo(4); // a = 4, b = 5;14 }15 }

default args grzebiel 21.11.2017 52 / 77

Page 117: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

argument redeclaration1 int foo(int a, int b = 5);23 int main()4 {5 foo(1); // a = 1, b = 567 int foo(int a, int b = 7);8 foo(2); // a = 2, b = 7;9 {

10 int foo(int a = 3, int b = 12);11 foo(); // a = 3, b = 12;12 // ::foo(); // compilation error13 ::foo(4); // a = 4, b = 5;14 }15 }

default args grzebiel 21.11.2017 52 / 77

Page 118: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

multiple declarations

1 #include <iostream>23 void foo(int d, float u = 3.14);4 void foo(int p = 5, float a);56 int main()7 {8 foo();9

10 return 0;11 }

default args grzebiel 21.11.2017 53 / 77

Page 119: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

multiple declarations

1 #include <iostream>23 void foo(int d, float u = 3.14);4 void foo(int p = 5, float a);56 int main()7 {8 foo();9

10 return 0;11 }

default args grzebiel 21.11.2017 53 / 77

Page 120: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

multiple declarations

1 #include <iostream>23 void foo(int d, float u = 3.14);4 void foo(int p = 5, float a);56 int main()7 {8 foo();9

10 return 0;11 }

default args grzebiel 21.11.2017 53 / 77

Page 121: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

CppCon 2017: Michael Price “Function default arguments:Slingshot or Shotgun?”

default args grzebiel 21.11.2017 54 / 77

Page 122: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 auto

2 decltype

3 default args

4 rvalue reference

5 syntax

rvalue reference grzebiel 21.11.2017 55 / 77

Page 123: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 #include <iostream>2 #include <string>34 void foo(std::string&) {5 std::cout << "got lvalue "6 << std::endl;7 }89 void foo(std::string&&) {

10 std::cout << "got rvalue "11 << std::endl;12 }1314 int main() {15 std::string&& a{"this it the string"};16 foo(a);17 return 0;18 }

rvalue reference grzebiel 21.11.2017 56 / 77

Page 124: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 #include <iostream>2 #include <string>34 void foo(std::string&) {5 std::cout << "got lvalue "6 << std::endl;7 }89 void foo(std::string&&) {

10 std::cout << "got rvalue "11 << std::endl;12 }1314 int main() {15 std::string&& a{"this it the string"};16 foo(a);17 return 0;18 }

rvalue reference grzebiel 21.11.2017 56 / 77

Page 125: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 #include <iostream>2 #include <string>34 void foo(std::string&) {5 std::cout << "got lvalue "6 << std::endl;7 }89 void foo(std::string&&) {

10 std::cout << "got rvalue "11 << std::endl;12 }1314 int main() {15 std::string&& a{"this it the string"};16 foo(a);17 return 0;18 }

rvalue reference grzebiel 21.11.2017 56 / 77

Page 126: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 #include <iostream>2 #include <string>34 void foo(std::string&) {5 std::cout << "got lvalue "6 << std::endl;7 }89 void foo(std::string&&) {

10 std::cout << "got rvalue "11 << std::endl;12 }1314 int main() {15 std::string&& a{"this it the string"};16 foo(a);17 return 0;18 }

rvalue reference grzebiel 21.11.2017 56 / 77

Page 127: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 #include <iostream>2 #include <string>34 void foo(std::string&) {5 std::cout << "got lvalue "6 << std::endl;7 }89 void foo(std::string&&) {

10 std::cout << "got rvalue "11 << std::endl;12 }1314 int main() {15 std::string&& a{"this it the string"};16 foo(a);17 return 0;18 }

rvalue reference grzebiel 21.11.2017 56 / 77

Page 128: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 #include <iostream>2 #include <string>34 void foo(std::string&) {5 std::cout << "got lvalue "6 << std::endl;7 }89 void foo(std::string&&) {

10 std::cout << "got rvalue "11 << std::endl;12 }1314 int main() {15 std::string&& a{"this it the string"};16 foo(a);17 return 0;18 }

rvalue reference grzebiel 21.11.2017 56 / 77

Page 129: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

and we got

lvalue

rvalue reference grzebiel 21.11.2017 57 / 77

Page 130: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

and we got lvalue

rvalue reference grzebiel 21.11.2017 57 / 77

Page 131: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

rvalue reference grzebiel 21.11.2017 58 / 77

Page 132: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

The variable of type rvalue reference to type T is

an lvalue itself.

rvalue reference grzebiel 21.11.2017 59 / 77

Page 133: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

The variable of type rvalue reference to type T is

an lvalue itself.

rvalue reference grzebiel 21.11.2017 59 / 77

Page 134: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

rvalue reference grzebiel 21.11.2017 60 / 77

Page 135: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

Lets imagine that we are in alternative universe in whichvariable of type rvalue reference is always an rvalue

rvalue reference grzebiel 21.11.2017 61 / 77

Page 136: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

rvalue reference grzebiel 21.11.2017 62 / 77

Page 137: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 #include <iostream>2 #include <string>34 void foo(std::string&) {5 std::cout << "got lvalue "6 << std::endl;7 }89 void foo(std::string&&) {

10 std::cout << "got rvalue "11 << std::endl;12 }1314 int main() {15 std::string&& a{"this it the string"};16 foo(a);17 return 0;18 }

rvalue reference grzebiel 21.11.2017 63 / 77

Page 138: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 #include <iostream>2 #include <string>34 void foo(std::string&) {5 std::cout << "got lvalue "6 << std::endl;7 }89 void foo(std::string&&) {

10 std::cout << "got rvalue "11 << std::endl;12 }1314 int main() {15 std::string&& a{"this it the string"};16 foo(a);17 return 0;18 }

rvalue reference grzebiel 21.11.2017 63 / 77

Page 139: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 #include <iostream>2 #include <string>34 void foo(std::string&) {5 std::cout << "got lvalue "6 << std::endl;7 }89 void foo(std::string&&) {

10 std::cout << "got rvalue "11 << std::endl;12 }1314 int main() {15 std::string&& a{"this it the string"};16 foo(a);17 return 0;18 }

rvalue reference grzebiel 21.11.2017 63 / 77

Page 140: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

in this magical place

1 void foo(std::string s);23 void bar(std::string&& s)4 {5 foo(s);6 std::cout << s7 << std::endl;8 };

rvalue reference grzebiel 21.11.2017 64 / 77

Page 141: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

in this magical place

1 void foo(std::string s);23 void bar(std::string&& s)4 {5 foo(s);6 std::cout << s7 << std::endl;8 };

rvalue reference grzebiel 21.11.2017 64 / 77

Page 142: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

in this magical place

1 void foo(std::string s);23 void bar(std::string&& s)4 {5 foo(s);6 std::cout << s7 << std::endl;8 };

rvalue reference grzebiel 21.11.2017 64 / 77

Page 143: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

in this magical place

1 void foo(std::string s);23 void bar(std::string&& s)4 {5 foo(s);6 std::cout << s7 << std::endl;8 };

rvalue reference grzebiel 21.11.2017 64 / 77

Page 144: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

in this magical place

1 void foo(std::string s);23 void bar(std::string&& s)4 {5 foo(s);6 std::cout << s7 << std::endl;8 };

rvalue reference grzebiel 21.11.2017 64 / 77

Page 145: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

in this magical place

1 void foo(std::string s);23 void bar(std::string&& s)4 {5 foo(s);6 std::cout << s7 << std::endl;8 };

rvalue reference grzebiel 21.11.2017 64 / 77

Page 146: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

back to normal world

rvalue reference grzebiel 21.11.2017 65 / 77

Page 147: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 void foo(std::string&);2 void foo(std::string&&);3 //...45 template<typename T>6 void bar(const T& t)7 {8 foo(t);9 }

1011 template<typename T>12 void bar(T&& t)13 {14 foo(std::move(t));15 }

rvalue reference grzebiel 21.11.2017 66 / 77

Page 148: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 void foo(std::string&);2 void foo(std::string&&);3 //...45 template<typename T>6 void bar(const T& t)7 {8 foo(t);9 }

1011 template<typename T>12 void bar(T&& t)13 {14 foo(std::move(t));15 }

rvalue reference grzebiel 21.11.2017 66 / 77

Page 149: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 void foo(std::string&);2 void foo(std::string&&);3 //...45 template<typename T>6 void bar(const T& t)7 {8 foo(t);9 }

1011 template<typename T>12 void bar(T&& t)13 {14 foo(std::move(t));15 }

rvalue reference grzebiel 21.11.2017 66 / 77

Page 150: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 void foo(std::string&);2 void foo(std::string&&);3 //...45 template<typename T>6 void bar(const T& t)7 {8 foo(t);9 }

1011 template<typename T>12 void bar(T&& t)13 {14 foo(std::move(t));15 }

rvalue reference grzebiel 21.11.2017 66 / 77

Page 151: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 void foo(std::string&);2 void foo(std::string&&);3 //...45 template<typename T>6 void bar(const T& t)7 {8 foo(t);9 }

1011 template<typename T>12 void bar(T&& t)13 {14 foo(std::move(t));15 }

rvalue reference grzebiel 21.11.2017 66 / 77

Page 152: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 void foo(std::string&);2 void foo(std::string&&);3 //...45 template<typename T>6 void bar(const T& t)7 {8 foo(t);9 }

1011 template<typename T>12 void bar(T&& t)13 {14 foo(std::move(t));15 }

rvalue reference grzebiel 21.11.2017 66 / 77

Page 153: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

forwarding reference

1 void foo(std::string&);2 void foo(std::string&&);3 //...45 template<typename T>6 void bar(T&& t)7 {8 foo(std::forward<T>(t));9 }

rvalue reference grzebiel 21.11.2017 67 / 77

Page 154: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

forwarding reference

1 void foo(std::string&);2 void foo(std::string&&);3 //...45 template<typename T>6 void bar(T&& t)7 {8 foo(std::forward<T>(t));9 }

rvalue reference grzebiel 21.11.2017 67 / 77

Page 155: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

forwarding reference

1 void foo(std::string&);2 void foo(std::string&&);3 //...45 template<typename T>6 void bar(T&& t)7 {8 foo(std::forward<T>(t));9 }

rvalue reference grzebiel 21.11.2017 67 / 77

Page 156: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

this is not forwarding1 template<typename T>2 struct Foo3 {4 void bar(T&& t);5 };67 int main()8 {9 std::string s = "I don’t fit small string";

10 Foo<std::string> f;11 //f.bar(s); // compilation error12 f.bar(std::move(s));13 };

rvalue reference grzebiel 21.11.2017 68 / 77

Page 157: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

this is not forwarding1 template<typename T>2 struct Foo3 {4 void bar(T&& t);5 };67 int main()8 {9 std::string s = "I don’t fit small string";

10 Foo<std::string> f;11 //f.bar(s); // compilation error12 f.bar(std::move(s));13 };

rvalue reference grzebiel 21.11.2017 68 / 77

Page 158: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

this is not forwarding1 template<typename T>2 struct Foo3 {4 void bar(T&& t);5 };67 int main()8 {9 std::string s = "I don’t fit small string";

10 Foo<std::string> f;11 //f.bar(s); // compilation error12 f.bar(std::move(s));13 };

rvalue reference grzebiel 21.11.2017 68 / 77

Page 159: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

this is not forwarding1 template<typename T>2 struct Foo3 {4 void bar(T&& t);5 };67 int main()8 {9 std::string s = "I don’t fit small string";

10 Foo<std::string> f;11 //f.bar(s); // compilation error12 f.bar(std::move(s));13 };

rvalue reference grzebiel 21.11.2017 68 / 77

Page 160: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

this is not forwarding1 template<typename T>2 struct Foo3 {4 void bar(T&& t);5 };67 int main()8 {9 std::string s = "I don’t fit small string";

10 Foo<std::string> f;11 //f.bar(s); // compilation error12 f.bar(std::move(s));13 };

rvalue reference grzebiel 21.11.2017 68 / 77

Page 161: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

this is not forwarding1 template<typename T>2 struct Foo3 {4 void bar(T&& t);5 };67 int main()8 {9 std::string s = "I don’t fit small string";

10 Foo<std::string> f;11 //f.bar(s); // compilation error12 f.bar(std::move(s));13 };

rvalue reference grzebiel 21.11.2017 68 / 77

Page 162: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

wait a second

what if all I want is an rvalue referemce?

rvalue reference grzebiel 21.11.2017 69 / 77

Page 163: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 #include <type_traits>23 template <4 typename T,5 typename = std::enable_if_t<6 std::is_rvalue_reference<T>::value> >7 void bar(T&& t)8 {9 foo(std::move(t));

10 }

rvalue reference grzebiel 21.11.2017 70 / 77

Page 164: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 #include <type_traits>23 template <4 typename T,5 typename = std::enable_if_t<6 std::is_rvalue_reference<T>::value> >7 void bar(T&& t)8 {9 foo(std::move(t));

10 }

rvalue reference grzebiel 21.11.2017 70 / 77

Page 165: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 auto

2 decltype

3 default args

4 rvalue reference

5 syntax

syntax grzebiel 21.11.2017 71 / 77

Page 166: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 struct Foo2 {3 Foo() {4 std::cout << "default\n";5 }67 Foo(int) {8 std::cout << "param\n";9 }

10 };1112 int main()13 {14 int a = 5;15 { Foo(a); }16 return 0;17 }

syntax grzebiel 21.11.2017 72 / 77

Page 167: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 struct Foo2 {3 Foo() {4 std::cout << "default\n";5 }67 Foo(int) {8 std::cout << "param\n";9 }

10 };1112 int main()13 {14 int a = 5;15 { Foo(a); }16 return 0;17 }

syntax grzebiel 21.11.2017 72 / 77

Page 168: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 struct Foo2 {3 Foo() {4 std::cout << "default\n";5 }67 Foo(int) {8 std::cout << "param\n";9 }

10 };1112 int main()13 {14 int a = 5;15 { Foo(a); }16 return 0;17 }

syntax grzebiel 21.11.2017 72 / 77

Page 169: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 struct Foo2 {3 Foo() {4 std::cout << "default\n";5 }67 Foo(int) {8 std::cout << "param\n";9 }

10 };1112 int main()13 {14 int a = 5;15 { Foo(a); }16 return 0;17 }

syntax grzebiel 21.11.2017 72 / 77

Page 170: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 struct Foo2 {3 Foo() {4 std::cout << "default\n";5 }67 Foo(int) {8 std::cout << "param\n";9 }

10 };1112 int main()13 {14 int a = 5;15 { Foo(a); }16 return 0;17 }

syntax grzebiel 21.11.2017 72 / 77

Page 171: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 #include <stdio.h>2 int main()3 {4 int(x) = 38;5 x += 4;6 printf("%d", x);78 return 0;9 }

syntax grzebiel 21.11.2017 73 / 77

Page 172: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

syntax grzebiel 21.11.2017 74 / 77

Page 173: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 int foo(int);23 int main()4 {5 int (*x)(int) = foo;6 return 0;7 }

syntax grzebiel 21.11.2017 75 / 77

Page 174: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 int foo(int);23 int main()4 {5 int (*x)(int) = foo;6 return 0;7 }

syntax grzebiel 21.11.2017 75 / 77

Page 175: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 int foo(int);23 int main()4 {5 int (*x)(int) = foo;6 return 0;7 }

syntax grzebiel 21.11.2017 75 / 77

Page 176: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 struct Foo2 {3 Foo() {4 std::cout << "default\n";5 }67 Foo(int) {8 std::cout << "param\n";9 }

10 };1112 int main()13 {14 int a = 5;15 { Foo(a); }16 return 0;17 }

syntax grzebiel 21.11.2017 76 / 77

Page 177: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

1 struct Foo2 {3 Foo() {4 std::cout << "default\n";5 }67 Foo(int) {8 std::cout << "param\n";9 }

10 };1112 int main()13 {14 int a = 5;15 { Foo(a); }16 return 0;17 }

syntax grzebiel 21.11.2017 76 / 77

Page 178: - The worst feature of modern C++: default behavior di… · The worst feature of modern C++: default behavior Marcin Grzebieluch grzebieluch@me.com 21.11.2017 grzebiel 21.11.2017

?syntax grzebiel 21.11.2017 77 / 77