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



Wednesday, October 26, 2016

C++ tips, 2016 Week 42 (17-Oct - 23-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. Using parameter pack to write one_of
This one is from Jason Turner (here and here). Instead of writing:
if (thing.x == 1 || thing.x == 2 || thing.x == 3)
you can write:

template<typename U, typename ... T> 
bool one_of(U&& u, T && ... t) 
{
  return ( (u == t) || ...  ); 
}

Tuesday, October 25, 2016

Starting C++ weekly posts

I volunteered to make daily C++ tips for the people development program at my work. And I decided that if I'm going to make them anyway it will be even better if I wrap them in a weekly post.

Because even after all this years in C++ there is still much to learn ( I cant escape the feeling that I'm complete newbie ) the tips will be part what I currently read from the news feed, part of what I think is important and part something-I-just-discovered-that-I-must-have-known-years-ago.  My definition of tip is a small piece of knowledge that can be useful in day-to-day programming or it can broaden your perspective and give you directions to more bigger knowledge. That is why I'll try to keep the scope as broad as possible (and because I'm the encyclopedist type of person - my brain easily collects random pieces of knowledge but some times not the things I want or have to remember). Wish me luck and discipline to keep this going.

Full list of all C++ weekly tips posts:

Saturday, August 27, 2016

C++'s syntactic sugars

Lets first look at the Wikipedia definition of syntactic sugar:
Syntactic sugar is syntax within a programming language that is designed to make things easier to read or to express.
For the first syntactic sugar we have to go back in time. In C a[i] is syntactically equivalent to *(a + i) which allows us to write this:
int a[5];
a[2] = 1;
but also this:
3[a] = 5;
Which more than perfectly illustrates what a syntactic sugar is.

C++11 introduced the range-based for loop:
std::vector<int> v = {0, 1, 2, 3, 4, 5};
for (const int& i : v) // access by const reference
    std::cout << i << ' ';