Thursday, December 29, 2016

C++ tips, 2016 Week 51 (19-Dec - 25-Dec-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. The first rule about performance

Thursday, December 22, 2016

C++ tips, 2016 Week 50 (12-Dec - 18-Dec-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. Inherited constructors

Inherited constructors are used to reduce the boilerplate code in the case when the delivered class does not have new data members. In the case of this base class:

class Base 
{ 
public:
Base(int x, int y) : x_(x), y_(y) {} 
public: 
int x_; 
int y_; 
};

We can deliver from it:

class Derived : public Base 
{ 
using Base::Base; 
};

Thursday, December 15, 2016

C++ tips, 2016 Week 49 (5-Dec - 11-Dec-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. Capturing members by value

Imagine you want to return a lambda from an object that does some computations later but captures part of the state of that object:

class Boo { 
public: 
auto getValueMultiplier() { 
return [=](int multiplier) { 
return value * multiplier; 
}; 
} 
private: 
int value = 5; 
};

Thursday, December 8, 2016

C++ tips, 2016 Week 48 (28-Nov - 4-Dec-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. std::ratio

std::ratio is a class template for compile time rational arithmetics. Coupled with several alias templates for operations like std::ratio_add, std::ratio_multiply, etc you can do compile time arithmetics with rational numbers:

Thursday, December 1, 2016

C++ tips, 2016 Week 47 (21-Nov - 27-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. C++ is not going anywhere

That's the tip - it is a motivational one. I hear so many people announcing the death of C++ and how it is in decline and it is over. Even from within our community. Yet now there are more C++ developers than ever. Yes - other languages grow faster and the share of C++ is not growing but that is OK. It should be expected - we cant use C++ for everything. It does not make any sense.

The Committee is doing tremendous amount of work to add new features to the language without breaking 20+ years legacy code bases. Look at the current state (from Herb Sutter's Trip report: Fall 2016 ISO C++ standards meeting (Issaquah) ):

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 << ' '; 

Sunday, August 7, 2016

Code colorization

This month I started a new job and my new colleagues got a little bit shocked by how I am abusing the MSVC editor colors. My source looks like this:



Thursday, May 19, 2016

Keeping track of C++

During my ongoing push in the pursuit for knowledge in the C++ domain I identified various channels of information flow. They may be easily adoptable to any other domain but I'll keep my focus on C++. Let me describe them and elaborate a little bit.

Thursday, April 28, 2016

Hello!

I have an idea for a C++ blog. I've always was good at extracting the essence from things so I'll try to use that skill in writing articles about various topics about C++.

I plan to structure some articles this way - first brief introduction, than very short essence of what's most important to remember (those posts will have tag "compressed C++") than several parts each filled with more and more details but each being independent meaning that it should be extractable from the article and still readable and complete. At least that's the intend and its more an exercise than and educational effort.

I'm working on one article now so wish me luck and lets see what's going to happen.

The other articles will be as my inspiration dictates me.

Thanks.