Storage duration and linkage in C++

In the vernacular they say stack and heap, but the standard call this automatic and dynamic:

(more…)

QT signal performance significantly degrades when the number of the connections grow.

In my previous post I compared the performance of a single QT connection with the performance of a notification mechanism based on virtual functions.

In this post I’ll demonstrate how the performance of QT signals degrade when the number of the connections grow. Briefly saying a QT signal becomes 57 times slower than a virtual function.

Below I described my experimentations and provided the full source code.

(more…)

QT signal is ten times slower than a virtual function

I defined a QT Object that contains an integer value and uses QT signal along with awl::Observable based on virtual functions to notify the subscribers when the value changes:

#include "Awl/Observable.h"

#include <QObject>

namespace test
{
    class NotifyValueChanged
    {
    public:

        virtual void onValueChanged() = 0;
    };
    
    class ValueObject : 
        public QObject,
        public awl::Observable<NotifyValueChanged>
    {
        Q_OBJECT

    public:

        Q_PROPERTY(int value READ value WRITE setValue NOTIFY valueChanged)

        void notifyValueChanged()
        {
            Notify(&NotifyValueChanged::onValueChanged);
        }

    signals:

        void valueChanged();

(more…)

How we use forwarding references with ranges in C++

The parameter or print function is a forwarding reference because the range can change while being iterated, but the current value is l-value reference:

#include <string>
#include <vector>
#include <ranges>
#include <iostream>

template <class R, class Value>
concept range_over = std::ranges::range<R> &&
    std::same_as<std::ranges::range_value_t<R>, Value>;

template <range_over<std::string> Range>
void print(Range&& range)
{
    for (const std::string& val : range)
    {
        std::cout << val << std::endl;
    }
}
(more…)

Value category is a property of an expression in C++ or std::forward explained

The code below prints l-value:

#include <iostream>

namespace
{
    void foo(const int&)
    {
        std::cout << "const l-value" << std::endl;
    }

    void foo(int&)
    {
        std::cout << "l-value" << std::endl;
    }

    void foo(int&&)
    {
        std::cout << "r-value" << std::endl;
    }
}
(more…)

The right way to capture std::unique_ptr into a lambda expression

Starting from C++14:

#include <string>
#include <memory>

int main()
{
    std::unique_ptr<std::string> p = std::make_unique<std::string>("abc");

    auto func = [p = std::move(p)]()
    {
    };

    func();

    return 0;
}
(more…)

MSVC 2022 compiler bug

Static asserts in the code below do not fail with MSVC 19.32.31332 for x86:

#include <string>

int main()
{
    std::string v = "abc";

    auto func = [v]()
    {
        auto& l_ref = v;

        static_assert(std::is_same_v<decltype(l_ref), std::string&>);

        auto&& r_ref = std::move(v);

        static_assert(std::is_same_v<decltype(r_ref), std::string&&>);
    };

    func();

    return 0;
}

that is MSVC compiler bug. With GCC both l_ref and r_ref are const references.

Thanks to stackoverflow.com.

Reported this to Microsoft.

(more…)

Switching to PHP 7.4 on Ubuntu 16.04

Some WordPress plugins stopped working with PHP 7.0 on my Ubuntu 16.04 server and I switched to PHP 7.4 with the following commands:

sudo add-apt-repository ppa:jczaplicki/xenial-php74-temp
sudo apt-get update
sudo apt-get install php7.4 php7.4-fpm php7.4-cli php7.4-mysql php7.4-dom php7.4-mbstring -y
service php7.4-fpm status
cd /etc/php/7.4/fpm/pool.d
sudo mv www.conf www.conf.dist
sudo mv ../../../7.0/fpm/pool.d/devnote.conf .
sudo service php7.0-fpm reload
sudo service php7.4-fpm reload
(more…)

An example of how r-value reference works in C++

An example of extending the lifetime of a temporary:

#include <iostream>

int main()
{
    int a = 1;
    int&& b = std::move(a);
    int&& c = b + 1;

    b = b + 1;
    c = c + 1;

    std::cout << a << b << c;
}

The output is:

223
(more…)

QObject::sender() is a slow function

I did not expect that its share of CPU usage in my app is 12.15%:

Why does it iterate over some collection? Isn’t there only one sender in a thread?

(more…)