Category : c++20

Consider the following code which attempts to implement a partial specialization of class Bar. In the first case, the foo member function is defined inline and in the second case out of line. The out of line definition produces a compile error which I cannot figure out: error: out-of-line definition of ‘foo’ from class ‘Bar<T>’ ..

Read more

I am using the neat fmt library, which in its version 8, does compile-time checking of its format string if the compiler supports the relevant features. I would, at some point, like to write the following code: throw my_exception("error: {}", 123); Sadly, the naive implementation: struct my_exception : std::runtime_error { template<typename… Args> my_exception(Args&&… args) : ..

Read more

According to cppreference, there are the following initialization types: Value initialization, e.g. std::string s{}; Direct initialization, e.g. std::string s("hello"); Copy initialization, e.g. std::string s = "hello"; List initialization, e.g. std::string s{‘a’, ‘b’, ‘c’}; Aggregate initialization, e.g. char a[3] = {‘a’, ‘b’}; Reference initialization, e.g. char& c = a[0]; From time to time, reading about something ..

Read more

How do I join two views using std::ranges? In ranges-v3 the views are joined with views::concat(), and I cannot figure out how to do that with std::ranges. #include <string> #include <iostream> #include <vector> #include <ranges> using namespace std; using namespace views; void snake_to_camel() { auto transform = views::transform; auto const s = string{"feel_the_force"}; auto words ..

Read more

I’m using this xor template to obfuscate some strings, the code is compiling and working, however, IntelliSense throws a lot of ‘errors’ in the console: #include <algorithm> #include <string> template<std::size_t N, class T = std::string> struct BaseXorString { using string_type = T; using char_type = string_type::value_type; char_type encrypted[N]{}; static constexpr unsigned char key = "…"; ..

Read more

Consider struct full { struct basic { int a = 1; } base; int b = 2; }; void example() { alignas(full) std::byte storage[/* plenty of storage */]; full * pf = new (storage) full; basic * pb = &pf->base; new (storage) basic; // supposedly ends ‚Äčlifetime of *pf (right?) // if doesn’t, suppose we ..

Read more