Thursday, March 23, 2017

C++ tips, 2017 Week 9 (27-Feb - 5-Mar-2017)

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 don't have it in your company start it. 
List of all weekly posts can be found here.


1. std::accumulate

std::accumulate is a fundamental STL algorithm that uses operator+ on the elements of a range or  applies a custom provided operator:

Thursday, March 16, 2017

C++ tips, 2017 Week 8 (20-Feb - 26-Feb-2017)

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 don't have it in your company start it. 
List of all weekly posts can be found here.

1. bitset

std::bitset is a container for bits. Unlike the infamous std::vector<bool> it size is set at compile time so bitsets with different sizes are different types. It is designed to replace C-style more-<<,>>,|,&-than-program-expressions storing of binary flags in a long for example. It has operator[], bound checking tests, evaluating if all, none or any of the bits are true. It can also count the bits set to true so if are asked during an interview "How to count the true bits in a sequence of chars?" you can show the depth of your STL knowledge by answering something like "Well first I'll use std::bitset::count to write the most readable solution, then use it for baseline and then use std::array<char, 256> with precomputed number of bits for each index"

Monday, March 6, 2017

C++ tips, 2017 Week 7 (13-Feb - 19-Feb-2017)


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 don't have it in your company start it. 
List of all weekly posts can be found here.

1. Behavior

The C++ Standard defines several levels of behaviors that one should be aware of. If the program is well-formed (it follows the Standard syntax and compiles):
  • Specified - well... as the name suggests you can expect things to happen exactly as described in the Standard
  • Implementation defined - the Standard describes the outcome but how the input will be transformed into the output is up to the implementers. This is why for example
    std::map implementations differ between compiler vendors - it's up to them how to implement it.
  • Unspecified - Similar to implementation defined but the Committee intentionally left it undocumented. It is well-formed but you as a C++ user should not care exactly what it is. Take the returned type of std::bind for example. You can create a variable from what std::bind returns using auto and you can wrap it in std::functions type-erasing it but what it is exactly should not concern you.
  • Undefined behavior - a.k.a. the mother of all confusion. Basically, means that you should not make assumptions about what will happen in an undefined behavior situation and plan accordingly. It is usually for a reason - often we do not want to enforce too strict requirements on behavior [that introduce unnecessary functionality or disable compiler optimizations]. I think of it as an extreme variant of "don't pay for what you don't use". Reading from uninitialized local variables, for example, is UB. Yes, the Standard could have required all variables to be default initialized but that is often unnecessary and forces the compiler to default initialize them thus reducing the performance.