Why is there no support for concatenating std::string and std::string_view?

The reason for this is given in n3512 string_ref: a non-owning reference to a string, revision 2 by Jeffrey Yasskin: I also omitted operator+(basic_string, basic_string_ref) because LLVM returns a lightweight object from this overload and only performs the concatenation lazily. If we define this overload, we’ll have a hard time introducing that lightweight concatenation later. … Read more

polymorphic_allocator: when and why should I use it?

Choice quote from cppreference: This runtime polymorphism allows objects using polymorphic_allocator to behave as if they used different allocator types at run time despite the identical static allocator type The issue with “regular” allocators is that they change the type of the container. If you want a vector with a specific allocator, you can make … Read more

structured bindings: when something looks like a reference and behaves similarly to a reference, but it’s not a reference

I wrote this yesterday: decltype(x), where x is a structured binding, names the referenced type of that structured binding. In the tuple-like case, this is the type returned by std::tuple_element, which may not be a reference even though the structured binding itself is in fact always a reference in this case. This effectively emulates the … Read more

Why doesn’t an if constexpr make this core constant expression error disappear?

The standard doesn’t say much about the discarded statement of an if constexpr. There are essentially two statements in [stmt.if] about these: In an enclosing template discarded statements are not instantiated. Names referenced from a discarded statement are not required ODR to be defined. Neither of these applies to your use: the compilers are correct … Read more

Try to understand compiler error message: default member initializer required before the end of its enclosing class

This is a clang and gcc bug, we have a clang bug report for this: default member initializer for ‘m’ needed within definition of enclosing class for default argument of function which has the following example: #include <limits> class A { public: class B { public: explicit B() = default; ~B() = default; private: double … Read more

Why does aggregate initialization not work anymore since C++20 if a constructor is explicitly defaulted or deleted?

The abstract from P1008, the proposal that led to the change: C++ currently allows some types with user-declared constructors to be initialized via aggregate initialization, bypassing those constructors. The result is code that is surprising, confusing, and buggy. This paper proposes a fix that makes initialization semantics in C++ safer, more uniform,and easier to teach. … Read more

Simplest way to determine return type of function

You can leverage std::function here which will give you an alias for the functions return type. This does require C++17 support, since it relies on class template argument deduction, but it will work with any callable type: using ReturnTypeOfFoo = decltype(std::function{foo})::result_type; We can make this a little more generic like template<typename Callable> using return_type_of_t = … Read more