Tag: c++

SI++ library

Inspired by std::chrono idea, I wrote a small header-only library using modern C++ for strongly-typed unit metrics, mostly for distance and speed.

#include <sipp/sipp.hpp>

// ...

using namespace sipp::literals;

auto distance_in_km = 100.0_km;
sipp::Feet distance_in_feet = distance_in_km;

std::cout << "Distance in kilometers: " << distance_in_km.count() << std::endl;
std::cout << "Distance in feet: " << distance_in_feet.count() << std::endl;

auto casted = sipp::distance_cast<sipp::Meters>(5 * distance_in_km);
std::cout << "Multiplied kilometers and cast to meters: " << casted.count() << std::endl;

See more details in README.

Virtual functions in C++ – thoughts on performance penalty

Most C++ developers have heard about performance penalty of using virtual functions. But is it really so high that you should avoid this useful and sometimes the only possible language feature for a particular case?

First let’s see when this feature is really needed. When you wish to write a testable code, you likely have to deal with some sort of dependency injection.

Let’s say you have some class:

class SomeNastyBigClass
    m_dependency1(param1, param2, param3),
    m_dependency2(param1, param2, param3),
    m_dependency3(param1, param2, param3)
    // some other work for dependencies initialization

  // lots of different methods

  SomeDependencyClass1 m_dependency1;
  SomeDependencyClass2 m_dependency2;
  SomeDependencyClass3 m_dependency3;

Continue reading “Virtual functions in C++ – thoughts on performance penalty”