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.

Saturday, February 25, 2017

C++ User Group Sofia first year

Some time in November 2015 we decided that it is a shame that we do not have a C++ User Group in Sofia, Bulgaria, and we decided that instead of waiting to just make it. This is a retro and an overview of what happened in our first year. I hope you find it helpful and inspirational.

A year or two earlier I began to realize that I'm a dark matter developer and I started to read more articles, created dedicated dev twitter account, started listening to podcasts (thank you, CppCast!) - it was kind of waking up experience. When you plug into the C++ news stream you begin to realize that our community is currently flourishing and expanding. As the community grows there is need for more face to face communication -  nothing replaces face to face communication. Probably realizing that Jens Weller, a.k.a. Meeting C++, started to encourage and help creating C++ User Groups.

Monday, February 20, 2017

C++ tips, 2017 Week 6 (6-Feb - 12-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 dont have it in your company start it. 
List of all weekly posts can be found here.

1. Trailing return type

In C++11 we got trailing return type. That is we can write the return type at the end:


reason for this was lambdas with multiple return statements and function templates that that have return type dependent on the input parameter types:


however from C++14 we have Generalized return type deduction and now in most of the cases you can omit writing the trailing return type and just use auto.

Friday, February 10, 2017

C++ tips, 2017 Week 5 (30-Jan - 5-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 dont have it in your company start it. 
List of all weekly posts can be found here.

1. std::size, data, empty

From C++17 we are getting the free functions std::size, std::data, std::empty. They are similar to the free functions std::begin and std::end and if used with standard container just call the corresponding member function and work for C-style arrays if it makes sense. Possible std::size implementation for C-style arrays (container one just returns .size()):

(lets try GitHub Gist because Blogger formatting is driving me nuts)

Thursday, February 2, 2017

C++ tips, 2017 Week 4 (23-Jan - 29-Jan-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 dont have it in your company start it. 
List of all weekly posts can be found here.


1. .front() and .back()

The sequence containers from STL have these two functions for accessing the first or the last element of the container (or an analogue like std::priority_queue::top). They do not return iterator but a reference to the object in the vector.

We've all seen code like this:
std::vector<SomeElement> elements;

// later

SomeElement element;
// some calculations and initializations involving the element
// possibly passing it around by reference
elements.push_back(element);
//or
elements.emplace_back(std::move(element));
This is where .back() comes handy - instead of creating local variable and than inserting into the container sometimes it is better to insert it first and than work directly with the inserted object getting access to it by using back/front/top/etc:
elements.emplace_back();
auto& element = elements.back();

// same calculations and initializations involving the element
// possibly passing it around by reference
This is applicable in the simpler cases and there is no need to involve copy/move semantics.