C++ Weekly - Ep 313 - The `constexpr` Problem That Took Me 5 Years To Fix!

C++ Weekly With Jason Turner · 28K views · 2022 · 26:19

Open on YouTube

C, C++, Rust, Go & systems languagestutorial

Why Stack Overflow recommends it

Stack Overflow users link it when asking about or answering compile-time string construction, concatenation, and passing constexpr strings to run time.

Recommended by 2 people across 2 different months, 2023 to 2024.

What it is

Jason Turner of C++ Weekly walks through several C++20 techniques for building a std::string at compile time and then exposing it at run time as a std::string_view. He shows how copying the data into a std::array and returning it through a constant expression gets around the rule that new and delete must stay inside one constant evaluation.

What people said

Recommends Yksisarvinen · 2023-10 · Stack Overflow · on “c++ pass std::source_location::function_name() as string literal template argument”

Jason Turner has a long video on the problem of getting a const char* with length together in compile time: [this video] (not saying that your approach is wrong, just adding extra fluff for anyone interested in the problem)

Recommends Oersted · 2024-02 · Stack Overflow · on “What is the storage duration and lifetime of a non type template parameter and how can it be used for compile-time computation?”

In C++ Weekly - Ep 313 - The constexpr Problem That Took Me 5 Years To Fix! [this video] , Jason Turner demonstrates several compile-time techniques in order to construct a std::string at compile-time and then pass it to a std::string_view for usage at run time. The core issue is that, std::string is using new the compile-time constructed std::string must be used within a single expression as the new must have the corresponding delete within the same constant/compile-time context (sorry for the approximate wording). As the code is quite long, I put it at the end of my question. In order to do that, he first shows how the data from the std::string can be copied to an std::array by keeping it insid

Cites Yksisarvinen · 2024-01 · Stack Overflow · on “Compile Time string manipulation”

It's not going to be easy. Here's obtaining N concatenations of a string in compile time, and it takes ungodly amounts of boilerplate to do: [this video]

Cites lobelk · 2024-06 · Stack Overflow · on “How to join compile-time string-like objects while keeping the API simple?”

I am trying to concatenate string-like objects at compile-time. With the help of this post [LINK] , I came up with something like this: #include <cstddef> #include <utility> #include <algorithm> #include <array> #include <string_view> template <std::size_t N> class CharArray { private: std::array<char, N> _string; template <std::size_t S> friend class CharArray; template <std::size_t S1, std::size_t S2> constexpr CharArray(const std::array<char, S1> s1, const std::array<char, S2> s2) : _string() { std::copy(s1.begin(), s1.end() - 1, _string.begin()); std::copy(s2.begin(), s2.end() - 1, _string.begin() + S1 - 1); } public: constexpr CharArray(const char (&str)[N]) : _string() { std::copy(&str