6,129 questions
2
votes
0
answers
42
views
Can an exported function template invoke a module-local function using ADL?
Let's say that we have a simple main.cpp file which contains only a single call of a function bar which is defined in the module a:
import a;
int main() {
bar(1);
}
Then let's take a look at the ...
5
votes
6
answers
233
views
How to ensure order of template parameter pack with static_assert?
Let's say I have the following code:
#include <array>
#include <string>
#include <variant>
using namespace std::literals;
using known_types_t = std::variant<int, double, char>...
-3
votes
1
answer
112
views
How to overload the newish 3-way operator for a simple class whose value is contingent on 2 numeric properties
Overloading 3-way comparison operator.
I am a bit confused when it comes to the 3-way comparison operator. I don't understand why the two return types, that I listed below, are used with the 3-way ...
4
votes
2
answers
148
views
Can I compare two view sentinels?
I am implementing type-erased iterators for code that uses std::views and have found a problem while trying to compare values that wrap sentinels. Basically, it seems that for some composition of ...
9
votes
1
answer
288
views
How can you prevent a destructor call from being reordered above an atomic write by the compiler?
Here is a possibly incorrect program for communicating between a function and a signal handler that might interrupt it. Assume that HandleSignal has been arranged to run as a signal handler. The ...
6
votes
1
answer
336
views
Is it possible to build a mutex from C++20 std::atomic without spinning to avoid use-after-free when deleting?
Before I get to my main question, I'm assuming that std::mutex::unlock() stops touching this as soon as it puts the mutex in a state where another thread might lock it - even though the call to unlock(...
0
votes
0
answers
132
views
Confusion on value initialization [duplicate]
I have the below code with the below output:
#include <iostream>
#include <string>
using namespace std;
class P {
public:
P() {
cout << "P() ctor" << ...
0
votes
5
answers
280
views
Generic mapping of enum class to int
Assume I have a bunch of different enum classes. Further I have a general handler that can work with int values. The handler is in an UI layer, that cannot be templated. It uses the int-converted ...
21
votes
4
answers
2k
views
How can I combine corresponding items from several vectors into a single vector of structs?
I have three std::vectors,
std::vector<std::string> vec1{ "one", "two", "three" };
std::vector<std::string> vec2{ "1", "2", "3" }...
21
votes
1
answer
653
views
Default equality operator for an empty union
C++ allows us to make union class type without any data members. (cppreference even contain an example with it.)
Can one request the compiler to provide default implementation of equality comparison ...
3
votes
6
answers
200
views
Generalize a self referential struct construction using a variadic template
I have some tricky code that I need to initialize many objects that refer to each other. I want to allocate and construct them all at once, so I put them all in a struct.
It looks like this:
// User ...
-3
votes
1
answer
205
views
Splitting file content using C++20 ranges [closed]
I'm trying to iterate through a text file's lines using ranges:
auto iter_lines(std::ifstream& file)
{
auto lines = std::ranges::istream_view<char>(file) |
std::views::lazy_split(...
2
votes
1
answer
236
views
constexpr unsigned int * unsigned long not evaluated as unsigned long?
The following integer power function returns 0 when the result should be greater than 2^32 if the base argument is unsigned int, but works fine when the base argument type is changed to an unsigned ...
6
votes
0
answers
447
views
How to formally prove that a statement after a spin loop isn't executed unless another thread exchanged first, with relaxed atomic exchange + store
Consider this example:
#include <atomic>
#include <thread>
#include <cassert>
int main(){
std::atomic<int> v = 0;
std::atomic<bool> flag = false;
std::thread ...
5
votes
1
answer
175
views
Is it okay to pass projection as predicate argument to std::ranges::any_of/all_of/none_of?
This works, but is it legal/safe?
(i.e.: Is it okay to pass projection as predicate argument to std::ranges::any_of/all_of/none_of?)
#include <ranges>
#include <iostream>
#include <...
-1
votes
1
answer
137
views
Normalize signed integer values to range [0.0..1.0]
We read files that can contain all kinds of data types, which we normalize before further processing.
This also works with MSVC, GCC, and Clang on my PC. However, GCC does strange things on our CI.
...
1
vote
0
answers
57
views
What is the category of transform view based on vector? [duplicate]
What is the category of std::views::transform based on a std::vector? What is the category of the view's iterator in the following code?
#include <iterator>
#include <memory>
#include <...
21
votes
2
answers
2k
views
Why does std::set::contains() call the spaceship operator twice on a target element?
Consider:
#include <print>
#include <set>
struct Num {
auto operator <=> (const Num& other) const{
std::println ("{} <=> {}", val, other.val);
...
4
votes
1
answer
496
views
Converting pointer-to-member to member index using Boost.PFR without creating an object
Consider the following struct:
struct Particle
{
float scale;
float opacity;
float rotation;
};
I want the following to compile:
static_assert(indexOf<&Particle::scale>() == 0);
...
10
votes
0
answers
176
views
Out of line definitions for members of constrained partial specializations
MSVC doesn't accept this code, even the latest version on Godbolt:
#include <type_traits>
template <class R>
struct A {
A();
};
template <class R>
requires std::is_reference_v&...
5
votes
1
answer
167
views
Are constraints in C++ std::ranges::ref_view constructor redundant, and is the forwarding reference necessary?
The libstdc++ implementation of std::ranges::ref_view, following the C++ standard, includes the following code:
template<range _Range> requires is_object_v<_Range>
class ref_view : ...
2
votes
1
answer
176
views
How to use `static_assert(std::forward_iterator<my_iterator<...>>);`
I found some (for me) unexplainable behaviour in VS2022 and C++20 with static_assert(std::forward_iterator<my_iterator<...>>), looks to me broken or I'm not using it in a correct fasion. ...
0
votes
2
answers
166
views
Does casting between T* and a union{pointer, T}* violate strict aliasing rules in C++?
I am implementing a non-intrusive idle objectpool in C++ using a union inside a struct, Does this code violate the strict aliasing rule?
The goal is to reuse memory for objects of type T while also ...
9
votes
2
answers
586
views
How can I flatten a circular dependency using C++20 modules?
My game engine in C++ using modules from C++20. class A contains unique_ptr of class B, class B contains regular pointer to class A and unique_ptr to class C, and this structure continues for 4 levels ...
0
votes
0
answers
134
views
I still don't quite understand the difference between memory_order_acq_rel and memory_order_seq_cst?
I read some QA about these two operations. But I still don't understand.
acquire-release-versus-sequentially-consistent-memory-order
Can I understand the difference between memory_order_acq_rel and ...