6,129 questions
2
votes
1
answer
295
views
Why is it UB to pass a char array as the argument of placement new?
I think the following code is ubiquitous:
struct A { ... };
char buf[1024];
auto ptr = new (buf) A(...);
However, new (buf) A(...); is UB. https://en.cppreference.com/w/cpp/language/lifetime.html ...
11
votes
1
answer
564
views
Is this concept satisfaction a bug in GCC or did I invoke undefined behavior?
Here's the test code:
#include <concepts>
template<typename T>
concept Printable = requires(T obj) {
{ obj.print() } -> std::same_as<void>;
};
template<Printable T>
...
1
vote
0
answers
73
views
Nominal typing with C++20 concepts [duplicate]
I'm trying to replace virtual base classes with concepts in a few places in my codebase where dynamic polymorphism isn't actually being used. However I still want it to be nominally typed. In ...
3
votes
1
answer
152
views
Why add redundant `std::constructible_from<T>` to the concept `std::default_initializable`?
At the cppref page on the concept std::default_initializable, I saw the following code:
template<class T>
concept default_initializable =
std::constructible_from<T> && ...
1
vote
0
answers
138
views
LNK2001 when using MSVC, C++20 vformat, C++20 module, and dll
I'm encountering linker errors when using std::vformat inside an exported function from a C++20 module in a DLL. The minimal project setup to reproduce this issue is as follows:
MainSolution
DLL ...
1
vote
1
answer
113
views
Why does the friend template function have different behaviors for struct and class?
#include <array>
template<std::size_t tc_n>
requires (tc_n > 0)
struct StringLiteral final {
std::array<char, tc_n> value{};
consteval StringLiteral(char const (&sl)[...
1
vote
1
answer
80
views
When use coro library, got error: ‘io_scheduler’ is not a member of ‘coro’
In my CMake project I use "coro" library. But when compile the project I got following errors:
/root/uwu/include/picohttp/server.hpp:58:58: error: ‘io_scheduler’ is not a member of ‘coro’
...
1
vote
1
answer
128
views
Is the result of std::ranges::sort well defined for ranges of types supporting only partial_ordering and having indeed unordered elements at runtime?
Here's a relatively short example:
#include <cassert>
#include <algorithm>
#include <compare>
#include <iostream>
#include <optional>
#include <vector>
struct Foo {...
6
votes
1
answer
281
views
Is there a problem with fmt format_as in C++20?
I've got a simple code example that attempts to use a format_as() overload to format a custom type (an enum); it outputs a std::string found in a map.
From the link above, the compiler is configured ...
0
votes
2
answers
185
views
Why does std::contiguous_iterator require the value type to be a reference?
From cppreference:
template< class I >
concept contiguous_iterator =
std::random_access_iterator<I> &&
std::derived_from</*ITER_CONCEPT*/<I>, std::...
3
votes
1
answer
131
views
How to gracefully shutdown asio::ssl::stream<asio::ip::tcp::socket> in C++ Asio? (shutdown always throws "stream truncated" exception)
I'm building a client-server project using standalone Asio (C++20) and OpenSSL. My client uses asio::ssl::stream<asio::ip::tcp::socket> for TLS connections.
After my client is done communicating ...
4
votes
1
answer
190
views
Spaceship comparison contradicts equality operator
I am defining a Point class where the "size" of a point is defined by its distance from the origin. I.e., a point p1 is "larger" than a point p2 if the sum of the squares of the ...
0
votes
3
answers
129
views
Polymorphic std::format that allows overriding
In order to use C++20's format capabilities with custom types, we have to provide template specializations of std::formatter for every type we want to format. The nice thing about this approach is ...
4
votes
4
answers
276
views
Check at compile time that a function call triggers a `static_assert(false)`
I have a template function with several type parameters and depending on those types, the function "calls" an static_assert(false). I want to know when my code fails to compile for specific ...
4
votes
1
answer
159
views
Copy constructor with impossible requires-constraint
In the following program, the copy constructor of struct S is declared with the constraint that the class is not copy constructible:
#include <concepts>
template <typename T>
struct S {
...
3
votes
1
answer
189
views
C++ format string compile-time validity check
I am trying to implement a C++ Translate function for localization.
// Language package, containing key-value pairs of translation, e.g.,
// g_LanguagePack["HELLO@1"] = "Hello, {}!"...
8
votes
2
answers
1k
views
Compiler is out of heap when compiling recursive consteval function
I'm writing a compile-time parser PoC (compiling a literal string to std::tuple) based on C++20 (or 23 if necessary). Currently gcc, clang, and MSVC all crash for this code, and MSVC gives useful ...
5
votes
2
answers
287
views
How does ODR work with lambdas at global scope
I have the following code -
template <typename T, typename F>
struct unique {};
template <auto F>
struct ltype {
using type = decltype(F);
};
using p = unique<int, ltype<[](){}&...
0
votes
0
answers
174
views
Why does my Double Dabble implementation that processes 32 bits a time give wrong result but 16 bits version is correct?
I am trying to implement Double Dabble algorithm to convert a BigInt implemented in std::vector<uint32_t> to decimal string and back.
I have already implemented a working version, using 16 bit ...
2
votes
3
answers
244
views
How to declare static constexpr variable in C++?
I have a Foo class that includes an array and a static constexpr variable. As a general convention, I want to write public and private variables respectively. But, compiler error occurs 's_array_size' ...
6
votes
1
answer
102
views
user-written time zones in C++ with fixed offset
I have a UTC offset that is not known until run-time, and is not necessarily an
integral number of hours. The IANA time zone
database isn't a good match for this.
What is the simplest user-written ...
30
votes
1
answer
1k
views
How can I find the Nth weekday of the month using chrono?
There are lots of Stack Overflow Q&A's solving this problem in other languages, but not so much in C++.
How do I find the Nth weekday of the month using <chrono>? For
example, what is the ...
5
votes
1
answer
175
views
range-based for-loop in C++ over std::optional<Container> does not work
Let me start with a C++ code that simplifies my issues I faced in the actual code base. I compiled it with --std=c++20 and --std=c++17. The first for-loop below was okay; the second for-loop, which ...
24
votes
6
answers
5k
views
Why use a mutex and not a semaphore?
Looking in cppreference, it seems to imply a std::binary_semaphore may be more efficient than a std::mutex.
Is there any reason not to use a std::binary_semaphore initialized to 1 instead of a std::...
3
votes
3
answers
114
views
Compiler data type errors when calling a template function
template<typename T, typename U>
void add_property_immutable(T property_name, U property_value) {
auto get_v8_data_type = []<typename X>(X&& property_name_or_value) -> int {
...