Category Archives: C++

Move assignment operator in C++

Implicitly-declared move assignment operator

If no user-defined move assignment operators are provided for a class type, and all of the following is true:

(more…)

Version tolerant serialization in C++

Last time I have been working on a C++ binary serialization framework that allows to serialize simple data structures with a few lines of code. First, you add AWL_REFLECT macro to all your structures as follows:

#include "Awl/Reflection.h"
#include <string>
#include <vector>
#include <set>

struct A
{
    int a;
    bool b;
    std::string c;
    double d;

    AWL_REFLECT(a, b, c, d)
};
(more…)

What is the type of a string literal in C++?

The code below compiles and the asserts do not fail:

#include <iostream>

int main()
{
    auto a = "str";
    const auto& ar = "str";
    const char* b = "str";
    auto const &c = a;
    auto* d = &a;
    const auto e = 2;

    static_assert(std::is_same_v<const char(&)[4], decltype("str")>);
    static_assert(std::is_same_v<const char*, decltype(a)>);
    static_assert(std::is_same_v<const char(&)[4], decltype(ar)>);
    static_assert(std::is_same_v<const char* const&, decltype(c)>);
    static_assert(std::is_same_v<const char**, decltype(d)>);
    static_assert(std::is_same_v<const int, decltype(e)>);

    std::cout << "a: " << typeid(a).name() << std::endl;
    std::cout << "ar: " << typeid(ar).name() << std::endl;
    std::cout << "b: " << typeid(b).name() << std::endl;
    
    return 0;
}

And a and b have identical typeids.

An example of overloading operator << in C++

The code below is compiled successfully with both GCC and MSVC:

#include <iostream>
#include <sstream>

template <class C>
class basic_format
{
public:
    
    template <typename T>
    basic_format & operator << (const T & val)
    {
        out << val;
        return *this;
    }

    std::basic_string<C> str() const { return out.str(); }

    operator std::basic_string<C>() const { return str(); }

private:
    
    std::basic_ostringstream<C> out;
};
(more…)

Google Play Console displays std::exception message

My Android game crashed on a user’s device at the highlighted line:

GameMovement::GameMovement(GameField& field, CellCoord from, CellCoord to) :
    m_field(field),
    pBall(m_field.GetCell(from))
{
    GameGraph graph(&m_field, from, to);

    m_path.reserve(m_field.GetColumnCount() + m_field.GetRowCount());

    if (!graph.FindPath(m_path))
    {
        throw std::runtime_error("Path not found.");
    }

    if (m_path.size() < 2)
    {
        throw std::runtime_error("Wrong path.");
    }
}
(more…)

“One Edit Apart” task on Yandex interview

The task is to write one_edit_apart function that determines is it possible to make two given strings equal by replacing, adding or removing one symbol. The equal strings are considered to be equal, for example:

one_edit_apart("cat", "at") == true
one_edit_apart("cat", "cats") == true
one_edit_apart("cat", "cast") == true
one_edit_apart("cast", "cats") == false
one_edit_apart("cat", "cut") == true
one_edit_apart("cat", "dog") == false

The task is trivial, but Yandex interviewers require all the calculation to be done exactly in their inline editor without debugging and compiling and they do not accept the solution until it works for all possible input. The obvious solution is to truncate equal heads and equal tails and check if the lengths of remaining strings are not greater than 1, but if you forgot to handle the case when heads and tails intersect (with “a” and “aa”, for example) they will say “ooops…., you did a newbie mistake that 1-st year students usually do” and if this all takes more than 40 minutes you fail to pass the test. Below I provided the source code they are most likely to accept:

(more…)

Key knowledge about forwarding references in C++

The compiler deduces U as std::string& or as std::string depending on value category of the argument:

template <class U>
void f(U&& s)
{
    static_assert(std::is_same_v<U, const std::string&>);

    std::cout << std::forward<U>(s) << std::endl;
}

int main()
{
    const std::string& s = "abc";

    f(s);

    return 0;
}
(more…)

Building BOOST with MSVC2022

I built BOOST with MSVC2022 in a similar way I built it with MSVC2017 except that I did install, but not stage:

cd C:\dev\repos\boost_1_80_0

set OPENSSL_ROOT_DIR=C:/dev/libs/OpenSSL
set OPENSSL_USE_STATIC_LIBS=ON

bootstrap.bat

b2 install --prefix=C:/dev/libs/boost_1_80_0 --toolset=msvc-14.3 link=static runtime-link=static variant=release address-model=64
b2 install --prefix=C:/dev/libs/boost_1_80_0 --toolset=msvc-14.3 link=static runtime-link=static variant=debug address-model=64

I am not sure if OPENSSL_ROOT_DIR takse an effect because boost::asio is header-only (but it depends on system and thread modules).

(more…)

Specialization of parameterless template function in C++

At least this compiles and works:

#include <iostream>
#include <memory>
#include <string>

template <class T>
std::shared_ptr<T> make_instance();

template <class T> requires std::is_default_constructible_v<T>
std::shared_ptr<T> make_instance()
{
    return std::make_shared<T>();
}
(more…)

Throwing an exception from a function parameter in C++

There is no difference between lines 82 and 92 in the code below with both MSVC2022 and GCC11:

#include <utility>
#include <stdexcept>
#include <iostream>

struct Object
{
    virtual const char* name() = 0;

    virtual ~Object() = default;
};

struct A : public Object
{
    const char* name() override
    {
        return "A";
    }
};
(more…)