Category Archives: Programming languages

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…)

“Cannot anchor to a null item” waring in QML

The following QML code

Callout
{
    id: call

    //margins should bound but not assigned
    anchors.topMargin: root.above ? undefined : yMargin
    anchors.bottomMargin: root.above ? yMargin : undefined

    ...
}

produced “Cannot anchor to a null item” warning and worked incorrectly.

I tried to replace it with AnchorChanges and PropertyChanges:

(more…)

A right way to query password from a console in JavaScript

import * as readline from 'node:readline/promises';
import { stdin as input, stdout as output } from 'node:process';
import { Writable } from 'node:stream';

var mutableStdout = new Writable({
  write: function(chunk, encoding, callback) {
    if (!this.muted)
      output.write(chunk, encoding);
    callback();
  }
});

mutableStdout.muted = false;

var rl = readline.createInterface({
  input: input,
  output: mutableStdout,
  terminal: true
});
(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…)

QML Application Style

There was qtquickcontrols2.conf in the root directory of my app sources with the following content:

; This file can be edited to change the style of the application
; See Styling Qt Quick Controls 2 in the documentation for details:
; https://doc.qt.io/qt-6/qtquickcontrols2-styles.html

[Controls]
;Style=Default
;Style=Universal
;Style=Material
Style=Fusion
;Style=iOS

[Universal]
Theme=Light
Accent=Steel

[Material]
;Theme=Light
Theme=Dark
;Accent=BlueGrey
;Primary=BlueGray
(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…)