Thursday, November 24, 2016

C++ tips, 2016 Week 46 (14-Nov - 20-Nov-2016)

This is part of my weekly C++ posts based on the daily C++ tips I make at my work. I strongly recommend this practice. If you dont have it in your company start it. 
List of all weekly posts can be found here.

1. Template normal programming

Taken from CppCon 2016 talk Template Normal Programming (part 1 of 2) by Arthur J. O'Dwyer


2. Currying

Friday, November 18, 2016

C++ tips, 2016 Week 45 (7-Nov - 13-Nov-2016)

This is part of my weekly C++ posts based on the daily C++ tips I make at my work. I strongly recommend this practice. If you dont have it in your company start it. 
List of all weekly posts can be found here.


1. Termination of a C++ program

There are multiple ways a C++ program may terminate. These include both normal and unexpected termination.
This GraphViz diagram shows the program termination flows as defined by the standard. It is a big one so probably better viewed on the github link.

Wednesday, November 9, 2016

C++ tips, 2016 Week 44 (31-Oct - 6-Nov-2016)

This is part of my weekly C++ posts based on the daily C++ tips I make at my work. I strongly recommend this practice. If you dont have it in your company start it. 
List of all weekly posts can be found here.

1. Locking the lambda type

This trick I saw in C++ Slack (great place, lots of interesting stuff). Its from Miro Knejp.

As we know the compiler generates different lambda types for different lambdas even if they are exactly the same:

auto lambda1 = []() {}; 
auto lambda2 = []() {}; 
std::cout << typeid(decltype(lambda1)).name() << '\n'; 
std::cout << typeid(decltype(lambda2)).name() << '\n';

This produces different results. Its implementation specific so it varies between compilers. However sometimes we want to put lambdas in a container and do something. We can use std::function and type erase them but it has some overhead (havent researched what exactly but I will, stay tuned).

So the trick here is to use a helper function that returns a lambda:

auto make_f = [](int multiplyer ) {     
return [multiplyer](const int& i) {  
return i * multiplyer;  
}; 
}; 

auto Funcs = std::vector<decltype(make_f(0))>(); 
Funcs.push_back(make_f(1)); 
Funcs.push_back(make_f(2));

Tuesday, November 1, 2016

C++ tips, 2016 Week 43 (24-Oct - 30-Oct-2016)

This is part of my weekly C++ posts based on the daily C++ tips I make at my work. I strongly recommend this practice. If you dont have it in your company start it. 
List of all weekly posts can be found here.

1. Infographics: Operation Costs in CPU Clock Cycles

Taken from the blog post of 'No Bugs' Hare