Non-defaulted operator doesn’t generate == and != [duplicate]

This is by design. [class.compare.default] (emphasis mine) 3 If the class definition does not explicitly declare an == operator function, but declares a defaulted three-way comparison operator function, an == operator function is declared implicitly with the same access as the three-way comparison operator function. The implicitly-declared == operator for a class X is an … Read more

How to use C++20 in CMake project

According to the C++ compiler support page (archive) on cppreference, C++20 <format> functionalities are supported by GCC libstdc++ 13; MSVC STL 19.29 (Visual Studio 2019 16.10); and Clang libc++ 14 (partial support) as of December 2022. You will be able to use #include <format> normally after upgrading to these versions. If upgrading is not an … Read more

Convert between std::u8string and std::string

UTF-8 “support” in C++20 seems to be a bad joke. The only UTF functionality in the Standard Library is support for strings and string_views (std::u8string, std::u8string_view, std::u16string, …). That is all. There is no Standard Library support for UTF coding in regular expressions, formatting, file i/o and so on. In C++17 you can–at least–easily treat … Read more

How can the type of braces influence object lifetime in C++?

Gcc is correct. The lifetime of the temporary will be extended only when using list-initialization syntax (i.e. using braces) in initialization of an aggregate. (since C++20) a temporary bound to a reference in a reference element of an aggregate initialized using direct-initialization syntax (parentheses) as opposed to list-initialization syntax (braces) exists until the end of … Read more

Constant integers and constant evaluation

Isn’t constant evaluation fun? There are a few places in the language where we try to do constant evaluation and if that fails, we fallback to doing non-constant evaluation. Static initialization is one such place, initializing constant integers is another. What happens with: int const i = f(); is that this could be constant evaluation, … Read more

How do I use C++ modules in Clang?

As of this commit, Clang has experimental support for the Modules TS. Let’s take the same example files (with a small change) as in the VS blog post about experimental module support. First, define the module interface file. By default, Clang recognizes files with cppm extension (and some others) as C++ module interface files. // … Read more

Portably detect __VA_OPT__ support?

Inspired by chris’s answer.1 #define PP_THIRD_ARG(a,b,c,…) c #define VA_OPT_SUPPORTED_I(…) PP_THIRD_ARG(__VA_OPT__(,),true,false,) #define VA_OPT_SUPPORTED VA_OPT_SUPPORTED_I(?) If __VA_OPT__ is supported, VA_OPT_SUPPORTED_I(?) expands to PP_THIRD_ARG(,,true,false,), so the third argument is true; otherwise, VA_OPT_SUPPORTED_I(?) expands to PP_THIRD_ARG(__VA_OPT__(,),true,false,), the third argument is false. Edit: As Edward Diener’s answer notes, GCC >= 8 issues a warning or error whenever it sees __VA_OPT__, … Read more