Andreas Fertig - Fast and Small
Andreas Fertig - Fast and Small
Andreas Fertig
https://www.AndreasFertig.Info
[email protected]
@Andreas__Fertig
fertig
adjective /ˈfɛrtɪç/
finished
ready
complete
completed
auto
decltype(auto)
1
2
3 int foo = 1;
4
5 auto a = foo;
6 decltype(auto) b = foo;
7
8 auto c = (foo);
9 decltype(auto) d = (foo);
10
11 ++foo;
12
13 printf(”a: %d b: %d c: %d d: %d\n”, a, b, c, d);
decltype(auto)
1
2
3 int foo = 1;
4
5 auto a = foo;
6 decltype(auto) b = foo;
7
8 auto c = (foo);
9 decltype(auto) d = (foo);
10
11 ++foo;
12
13 printf(”a: %d b: %d c: %d d: %d\n”, a, b, c, d);
$ ./a.out
a: 1 b: 1 c: 1 d: 2
decltype(auto)
1 #define MAX(x,y) (((x) > (y)) ? (x) : (y))
2
3 int foo = 1;
4
5 auto a = foo;
6 decltype(auto) b = foo;
7
8 auto c = MAX(a, b);
9 decltype(auto) d = MAX(a, b);
10
11 ++foo;
12
13 printf(”a: %d b: %d c: %d d: %d\n”, a, b, c, d);
decltype(auto)
1 #define RANDOM_MACRO(x) (x++)
2
3 int foo = 1;
4
5 auto a = foo;
6 decltype(auto) b = foo;
7
8 auto c = RANDOM_MACRO(foo);
9 decltype(auto) d = RANDOM_MACRO(foo);
10
11 ++foo;
12
13 printf(”a: %d b: %d c: %d d: %d\n”, a, b, c, d);
decltype(auto)
1 #define RANDOM_MACRO(x) (x++)
2
3 int foo = 1;
4
5 auto a = foo;
6 decltype(auto) b = foo;
7
8 auto c = RANDOM_MACRO(foo);
9 decltype(auto) d = RANDOM_MACRO(foo);
10
11 ++foo;
12
13 printf(”a: %d b: %d c: %d d: %d\n”, a, b, c, d);
$ ./a.out
a: 1 b: 1 c: 1 d: 2
decltype(auto)
1
2 #define RANDOM_MACRO(x) (++x)
3 int foo = 1;
4
5 auto a = foo;
6 decltype(auto) b = foo;
7
8 auto c = RANDOM_MACRO(foo);
9 decltype(auto) d = RANDOM_MACRO(foo);
10
11 ++foo;
12
13 printf(”a: %d b: %d c: %d d: %d\n”, a, b, c, d);
decltype(auto)
1
2 #define RANDOM_MACRO(x) (++x)
3 int foo = 1;
4
5 auto a = foo;
6 decltype(auto) b = foo;
7
8 auto c = RANDOM_MACRO(foo);
9 decltype(auto) d = RANDOM_MACRO(foo);
10
11 ++foo;
12
13 printf(”a: %d b: %d c: %d d: %d\n”, a, b, c, d);
$ ./a.out
a: 1 b: 1 c: 2 d: 4
return (x);
return (x);
range-based for
1 {
2 auto && __range = for−range−initializer;
3
4 for ( auto __begin = begin−expr,
5 __end = end−expr;
6 __begin != __end;
7 ++__begin ) {
8 for−range−declaration = *__begin;
9 statement
10 }
11 }
int main()
{
[] () {} ();
}
Lambdas
1 int main()
2 {
3 int x = 1;
4
5 auto lambda = [&]() { ++x; };
6
7 lambda();
8
9 return x;
10 }
Lambdas
1 int main()
2 {
3 std::string foo;
4
5 auto a = [=] () { printf( ”%s\n”, foo.c_str()); };
6
7 auto b = [=] () { };
8
9 auto c = [foo] () { printf( ”%s\n”, foo.c_str()); };
10
11 auto d = [foo] () { };
12
13 auto e = [&foo] () { printf( ”%s\n”, foo.c_str()); };
14
15 auto f = [&foo] () { };
16 }
Structured Bindings
1 struct Point
2 {
3 int x;
4 int y;
5 };
6
7 Point pt{1,2};
8 auto [ax, ay] = pt;
1 class Point {
2 public:
3 constexpr Point(double x, double y) noexcept : mX(x), mY(y) {}
4
5 constexpr double GetX() const noexcept { return mX; }
6 constexpr double GetY() const noexcept { return mY; }
7
8 constexpr void SetX(double x) noexcept { mX = x; }
9 constexpr void SetY(double y) noexcept { mY = y; }
10 private:
11 double mX, mY;
12 };
static
1 Singleton& Singleton::Instance()
2 {
3 static Singleton singleton;
4
5 return singleton;
6 }
static
“ tion (3.7.1) or thread storage duration (3.7.2) is performed the first time con-
trol passes through its declaration; such a variable is considered initialized
upon the completion of its initialization. [...]”
— N4640 § 6.7 p4 [2]
static - Block
1 Singleton& Singleton::Instance()
2 {
3 static bool __compiler_computed;
4 static char singleton[sizeof(Singleton)];
5
6 if( !__compiler_computed ) {
7 new (&singleton) Singleton;
8 __compiler_computed = true;
9 }
10
11 return *reinterpret_cast<Singleton*>(&singleton);
12 }
static - Block
Thread-safe?
static - Block
1 Singleton& Singleton::Instance()
2 {
3 static int __compiler_computed;
4 static char singleton[sizeof(Singleton)];
5
6 if( !__compiler_computed ) {
7 if( __cxa_guard_acquire(__compiler_computed) ) {
8 new (&singleton) Singleton;
9 __compiler_computed = true;
10 __cxa_guard_release(__compiler_computed);
11 }
12 }
13
14 return *reinterpret_cast<Singleton*>(&singleton);
15 }
I am Fertig.
https://cppinsights.io
Used Compilers
References
[2] Smith R., “Working Draft, Standard for Programming Language C++”, N4640, Feb. 2016. http://wg21.link/n4640
[3] Toit S. D., “Working Draft, Standard for Programming Language C++”, N3337, Jan. 2012. http://wg21.link/n3337
Images:
4: Franziska Panter
36: Franziska Panter
Upcoming Events
■ C++: Fast and Small - What are the Costs of Language Features, Cpp Europe, February 26 2019
■ C++ Templates - die richtige Dosis kompakt, Seminar QA Systems, November 15 2018
Andreas holds an M.S. in Computer Science from Karlsruhe University of Applied Sci-
ences. Since 2010 he has been a software developer and architect for Philips Medical
Systems focusing on embedded systems. He has a profound knowledge of C++ and is a
frequent SG14 member.
He works freelance as a lecturer and trainer. Besides this he develops macOS applica-
tions and is the creator of cppinsights.io