cpprussia C++ Russia 2019 SPb (31.10.2019)

C++ tricks from Yandex.Taxi

img

Atypical and effective solutions for typical tasks:

  • Pimpl — but without dynamic allocations;

  • Fast logging — but usually without std::ostream and std::locale;

  • Conversions from JSON/XML/YAML/…​ to user-defined types — but with code reuse (almost a function overloading by result type);

When writing large frameworks, one has to face typical problems, solutions for which have long been known. However, these solutions are not effective enough and can be implemented much better.

We will talk about such effective tricks!

1) There is a famous Pimpl pattern:

<code> struct something {

int foo ();

private:

struct impl;
std

unique_ptr < const impl> impl_; // dynamic allocation :(

};

</code>

Let’s remove the dynamic allocation from it and add cache friendliness.

2) Logging of user types:

<code> std :: ostream & operator << (std :: ostream & os, const user_data_type & udt);

my_debug_logger << user_data_type {"foo", "bar"};

</code>

Let’s do it without std::ostream and without calculating the values of variables for the records that we do not log.

3) Conversion from JSON/XML/YAML/…​ into user-provided types:

<code>

user_data_type1 Parse (const Json & value);

user_data_type2 Parse (const Json & value);

template < class T> T Parse (const Json & value);

template <> user_data_type1 Parse (const Json & value);

template <> user_data_type2 Parse (const Json & value);

</code>

Let’s make it beautiful.