Перейти до основного вмісту

Публікації

D4: std::variant, std::visit, and return type deduction

Standard Library — std::variant, std::visit, and return type deduction C++17 introduced std::variant , a type-safe alternative to unions, alongside std::visit , which allows operations to be performed on the currently active alternative. C++20 extended std::visit with an overload accepting an explicit return type. Consider the following code: #include <variant> #include <type_traits> using Property = std::variant<int, float, bool>; auto visitor = [](auto value) { using T = decltype(value); if constexpr (std::is_same_v<T, int>) return value * 2; else if constexpr (std::is_same_v<T, float>) return value * 0.5f; else return value ? 1 : 0; }; int main() { Property property = 42; // Case A auto a = std::visit(visitor, property); // Case B auto b = std::visit<float>(visitor, property); property = 3.5f; // Case C auto c = std::visit<float>(visitor,...
Останні дописи

D3 Answer: Standard Library — std::mdspan, memory layouts, and data lifetime

Let's start by compiling and running this code. For instance: https://godbolt.org/ Code compiles and output is: Case A: 3 Case B: 1 Case C: 150589 The result of Case C is not guaranteed, as we will discuss later. According to the documentation std::mdspan is non-owning multi-dimensional array view that maps a multi-dimensional index to an element of the underlying storage. Since std::mdspan is non-owning, one needs to be careful with lifetime of the data that is accessed via std::mdspan . operator[] doesn't perform bounds checking of the array, leaving all responsibility to the programmer. C++26 introduces a new member function, at() , which performs bounds check but not everyone has the luxury to jump to the latest C++ version. std::mdspan is only a view representing the data so it doesn't change or rearrange data itself. Interface provides some flexibity in configuring layout for the data. Most basic would be std::layout_left an...

D3: Standard Library — std::mdspan, memory layouts, and data lifetime

C++23 introduced std::mdspan , a non-owning multidimensional view over existing data. Unlike std::span , it supports multiple dimensions and configurable memory layouts, including row-major and column-major layouts. std::mdspan is part of the published C++23 standard. Consider the following code: #include <cstddef> #include <mdspan> #include <vector> int main() { std::vector<int> data{0, 1, 2, 3, 4, 5}; using Shape = std::extents<std::size_t, 2, 3>; std::mdspan<int, Shape, std::layout_right> rowMajor(data.data()); std::mdspan<int, Shape, std::layout_left> columnMajor(data.data()); // Case A int a = rowMajor[1, 0]; // Case B int b = columnMajor[1, 0]; // Force vector reallocation data.reserve(data.capacity() + 1); // Case C int c = rowMajor[0, 0]; } Question How do the layout policies and ownership semantics of std::mdspan determine the behaviour...

D2 Answer: Core Language — if constexpr, std::is_constant_evaluated(), and if consteval

Below are answers to questions in D2: Core Language — if constexpr, std::is_constant_evaluated(), and if consteval . Q1: Assuming the user enters 10, what values will this program print, and why? The program will print 20 in both cases. Despite the temptation to assume that if constexpr will allow to distinguish compile time and runtime execution, that is not really the case. Function constexpr int Calculate(int value) can be called at either compile time or runtime, however condition if constexpr is always evaluated during compile time. constexpr int compileTime = Calculate(10); must be evaluated as a constant expression during compile time because variable compileTime declared as constexpr . All necessary information is available. On the other hand int runtime = Calculate(input); can't be evaluated during compile time, since input is not known then. However, even for a runtime call, the condition if constexpr (std::is_cons...

D1 Answer: Concepts, Constraint Subsumption, and Overload Resolution.

First of all, code doesn't compile.  error: call of overloaded 'processB(int)' is ambiguous It kind of suggests that the code is ill-formed. Answer to the first question.  Before I jump into further explanations, I would suggest 2 sources to look at:  CppReferences page -  Constraints and Concepts (C++20) CppCon video -  BackToBasics: Concepts Cpp reference article covers lots of ground and goes into great detail about Concepts. However, the language and examples used are sometimes hard to understand. That's where video comes in; Nicolai Josuttis's explanations of Concepts are great.  Going back to the questions in the initial article  D1: Core Language — Concepts, Constraint Subsumption, and Overload Resolution.   Code, as I already mentioned, is ill-formed and doesn't compile. Let's try to understand why.  Logically, we have declared similar concepts. SignedA and SignedB essentially define the same list of requirements for the fun...

D2: Core Language — if constexpr, std::is_constant_evaluated(), and if consteval

C++17 introduced if constexpr , allowing conditional compilation of branches based on compile-time conditions. C++20 introduced std::is_constant_evaluated() , providing a way to determine whether an expression is being evaluated in a manifestly constant-evaluated context. C++23 added if consteval , offering a more direct mechanism for distinguishing constant-evaluated execution from runtime execution. All three features are part of published C++ standards. Consider the following code, compiled with C++23: #include <type_traits> #include <iostream> constexpr int Calculate(int value) { if constexpr (std::is_constant_evaluated()) { return value * 2; } else { return value * 3; } } int main() { constexpr int compileTime = Calculate(10); int input; std::cin >> input; int runtime = Calculate(input); std::cout << compileTime << '\n'; std::cout << runtime << '\n'; } ...