895 questions
6
votes
1
answer
152
views
Is this const_cast in the context of type-erasure undefined behavior?
While looking at this example: https://github.com/PacktPublishing/Hands-On-Design-Patterns-with-CPP-Second-Edition/blob/main/Chapter06/09_function.C
which is an implementation of std::function with ...
1
vote
3
answers
274
views
Is std::unique_ptr needed on abstract class whose derived class only has a std::function member and no other data members?
I came across below code in a project I am working on. In class XYZ, I do not understand the need for std::unique_ptr on abstract class ABC. ABC has no data members; and the class Derived has only a ...
10
votes
1
answer
244
views
Cannot use std::not_fn with immediate functions
I try to apply std::not_fn wrapper to an immediate consteval function.
But even the simplest C++20 example
#include <functional>
consteval bool f() { return false; }
// ok in GCC and Clang, ...
5
votes
1
answer
226
views
Is there any reason to use virtual classes for factory method over map of functions in C++?
I'm implementing factory method for my polymorphic class.
The "traditional" way to do so would be to have two families of classes:
for objects themselves and for their factories.
In this ...
1
vote
1
answer
44
views
How to pass a pointer to member function to a template function
Here is my simplified code: S.h
class SBase {
...
public:
virtual std::vector<int32_t> getNumbersSet1();
virtual std::vector<int32_t> getNumbersSet2();
}
class SDerived : public ...
3
votes
1
answer
253
views
Differences between std::function_ref and delegates
What are the differences between the new std::function_ref, that was added to C++26, and a "delegate"? Are they the same?
By "delegate" I mean:
A type that has sizeof(...) == 16.
...
1
vote
1
answer
148
views
Handling std::array of structures with std::function
could you please help me how to handle (initialize, call) std::array of structs of std::function. Code like below:
class A
{
struct Functions
{
std::function<bool()> handler1;
std::...
4
votes
2
answers
183
views
C++: Best way to strengthen the type safety of assignment to std::function?
Summary: Earlier, I asked this question:
Why does C++ allow std::function assignments to imply an object copy?
Now that I understand why the code works the way it does, I would like to know how to fix ...
4
votes
1
answer
113
views
Why does C++ allow std::function assignments to imply an object copy?
My question concerns the line of code below marked with the // WHY??? comment.
Below is a toy example of some code I was expecting to break: a std::function variable is assigned a function pointer ...
6
votes
3
answers
413
views
Why doesn't the C++ standard library std::function just give error that target is not copy-constructible only IF it's being copy-constructed?
So this question explains why it's required that the target of a std::function requires that a valid copy constructor be callable. But the thing is, I know with the compilers I've dealt with that it's ...
0
votes
1
answer
308
views
'promise_type' is not a member of std::coroutine_traits
i have a class template called "guard_if" that is supposed to enable guards against function execution if specific criteria are not met. This class template takes two main parameters, a std::...
1
vote
2
answers
118
views
Assigning a class member function to a variable of type std::function<void(void *)>
I'm relatively new to C++, please forgive some imperfections in my formulation.
Summary:
Basically I have a class member function and a variable of type std::function<void(void *)>. I want to ...
0
votes
3
answers
249
views
Using std::bind, Lambda function or alternatives to pass a callback function in c-style to external dll
I want to handover a class member as a callback function in C-style to an external DLL.
What I got so far:
void MyCallback(double a, unsigned char b, unsigned char* c)
{
// do something
}
The DLL ...
0
votes
3
answers
99
views
Can packaged_task<void()> type objects be stored in queue?
This is the code for submitting tasks in the thread pool that I implemented, which is used to submit tasks to the thread pool. tasks_ is the task queue. This is the modified code.
using Task = std::...
1
vote
1
answer
125
views
How can I hide std::function internals in the stack trace?
When debugging and breaking inside a called std::function, 5 out of the 4 frames are unnecessary implementation details. E.g.:
#0 operator() # my lambda
#1 std::__invoke_impl
#2 std::__invoke_r
#3 ...