Category Archives: Programming languages

Negating the minimal integer results in an overflow in C++

The code below compiles with a warning:

#include <iostream>
#include <limits>
#include <cstdint>

int main()
{  
    int64_t m1 = -std::numeric_limits<int64_t>::min();
    int64_t m2 = -m1;
    
    std::cout << m1 << std::endl << m2 << std::endl << std::numeric_limits<int64_t>::max() << std::endl;
    
    return 0;
}
(more…)

Dragging a link from a browser to a QML app

When a link is dragged from a browser on Window 10 platform it contains the page title, but they did not make it accessible in QML:

import QtQuick 2.15
import QtQuick.Controls 2.15

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("Scroll")
    id: root

    DropArea {
        id: dropArea;
        anchors.fill: parent
        onEntered: {
            root.color = "gray";
            drag.accept (Qt.LinkAction);
        }
(more…)

The sum of signed and unsigned is unsigned in C++

#include <iostream>

template <typename T>
void PrintValue(T val)
{
    if constexpr (std::is_signed_v<T>)
    {
        std::cout << "signed";
    }
    else
    {
        std::cout << "unsigned";
    }

    std::cout << std::endl << val << std::endl;
}
(more…)

None of C++ compilers have heterogeneous lookup for unordered containers except MSVC

At 10/20/2020:

And std::vector is not constexpr yet:

And there is no text formatting also.

Coroutines:

std::chrono::days:

See C++20 library features compiler support.

Assignment a property of type function in QML

It is possible to do this in QML:

Item
{
    property var refreshChart: function () {}

    Component.onCompleted:
    {
        refreshChart = function ()
        {
            console.log("Hello!")
        }
    }
}
(more…)

Brain collapsing rules in C++

Consider the declaration of a class that contains a lambda function or a reference to it:

#include <utility>

template <class Func>
struct Holder
{
    Holder(Func && func) : m_func(std::forward<Func>(func))
    {
    }

    Func m_func;
};

template <class Func>
auto MakeHolder(Func && func)
{
    return Holder<Func>(std::forward<Func>(func));
}
(more…)

Why my QT app crashes at the destructor of std::thread on Android 10 devices?

I did some research on why my QT app crashes at the destructor of std::thread on Android 10 devices at the user side with the following call stack:

#00 /apex/com.android.runtime/lib64/bionic/libc.so (abort+160)
#01 /system/lib64/libc++.so (abort_message+232)
#02 /system/lib64/libc++.so (demangling_terminate_handler()+44)
#03 /system/lib64/libc++.so (std::__terminate(void (*)())+12)
#04 /system/lib64/libc++.so (std::terminate()+52)
#05 /system/lib64/libc++.so (std::__1::thread::~thread()+20)
#06 /apex/com.android.runtime/lib64/bionic/libc.so (__cxa_finalize+212)
#07 /apex/com.android.runtime/lib64/bionic/libc.so (exit+24)
#08 /data/app/com.domain.myapp-Rs_sm5VrLR1Jj8QW6oYByA==/lib/arm64/libplugins_platforms_qtforandroid_arm64-v8a.so

and figured out that its likely because std::thread destructor is being invoked while the thread is still joinable at some point of the application execution (thanks to G. M. on stackoverflow.com).

(more…)

C++ binary serialization speed is comparable with std::memmove

Measuring std::memmove speed

I wrote a simple test that outputs std::memmove speed to the console:

AWT_ATTRIBUTE(size_t, element_count, 1000000);

std::unique_ptr<uint8_t> p_src(new uint8_t[element_count]);
std::memset(p_src.get(), 25u, element_count);

std::unique_ptr<uint8_t> p_dst(new uint8_t[element_count]);

context.out << _T("std::memmove: ");

awl::StopWatch w;

std::memmove(p_dst.get(), p_src.get(), element_count);

ReportSpeed(context, w, element_count);

context.out << std::endl;

And the similar tests for std::memset and std::vector::insert.

(more…)

A funny example of C++ code that MS can compile :)

In the code below, static_assert operates on the addresses of local variables, but however MS compiles it and the assertion does not fail:

#include <tuple>
#include <string>
 
struct A
{
    int a;
    double b;
    std::string c;
};
 
void f()
{
    A a{ 1, 3.0, "abc" };
    constexpr auto t1 = std::tie(a.a, a.b, a.c);
    static_assert(&std::get<0>(t1) == &a.a);
}

Use the following command to compile this example:

cl /std:c++17 /EHsc a.cpp
(more…)

Basic ideas of version tolerant serialization in C++

Consider the following scenario: there was structure A in an old version of a C++ application:

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

An instance of A was serialized into a file in a binary format and after that the application was updated to a new version.

But in the new version of the application structure A was modified by adding fields d and e and deleting field a:

struct A
{
    int b;
    std::vector<int> d;
    bool e;
    std::string c;
};

and the new version of the application needs to deserialize an instance of its new structure A from the file containing old version of A.

(more…)