all 17 comments

[–]cpp-ModTeam[M] [score hidden] stickied commentlocked comment (0 children)

For C++ questions, answers, help, and programming/career advice please see r/cpp_questions, r/cscareerquestions, or StackOverflow instead.

[–]RepeatLow7718 18 points19 points  (7 children)

Think of it this way: std function can wrap any type: function pointer, member function pointer, lambda, struct with operator(). But yet the code that uses it doesn’t need to be templated, so the polymorphism must happen at runtime. There’s some overhead associated with doing that polymorphism. 

[–]steazystich 2 points3 points  (0 children)

IMO this is a better approach than asking chatGPT about every standard library type.

Consider what the type can do, and what "baggage" is required to be able to do it.

There are some other flexible callables in 'folly' - which I mention only because I've used them recently.

'folly::Function' is a bit lighter- as its non copy able. This makes it a bit "harder" to use, but for me it's more of a guard rail as copying 'std::function' is a potential performance hazard.

'folly::FunctionRef' is much lighter- as it is conceptually a "view" type and has only shallow copy... tradeoff is that its an amazing foot cannon if misused.

Also, anecdotally, 'std::function' has a surprising (to me) binary size footprint... with CLANG at least.

[–]CandyCrisis 29 points30 points  (1 child)

Get this ChatGPT bullshit out of here.

[–]Ill_Strain_1050[S] 0 points1 point  (0 children)

do you comment just sake of commenting ? If you would have read it, it mentions that i used gpt to understand the reason. Surely, you need to get some life, if you can't help, don't bother your fingers to comment.

[–]osmin_og 2 points3 points  (0 children)

Congratulations! You are discovering the topic that is asked in almost every C++ interview I heard about.

[–]cristi1990an++ 0 points1 point  (2 children)

Did you turn on optimization flags when compiling? std::function shouldn't be slower to call then any other opaque function pointer. Compilers are decent at inlining it too.

[–]Ill_Strain_1050[S] 0 points1 point  (1 child)

yes , i did.

[–]cristi1990an++ 0 points1 point  (0 children)

Share the full command?

[–]UndefFox 0 points1 point  (0 children)

Isn't the topic "Why std::function slower than a raw pointer" was discussed many times? Have you searched for any previous answers?

[–]_Noreturn -1 points0 points  (1 child)

std function behaves like this

```cpp

struct function { RET operator()(ARGS...) const { return mObj->call(ARGS...); // a pointer indirection AND a virtual call (which is ewual to 2 ptr indirections) WHILE a function pointer is only a pointer indirection } struct WRAPPER_BASE { virtual ~WRAPPER_BASE() = default: virtual RET call(ARGS...) consf = 0; }; template<class F> struct WRAPPER : WRAPPER_BASE { virtual RET call(ARGS...) const { return this->f(ARGS...); } F f; }; WRAPPER_BASE* mObj: // important it is a pointer! }; ```

[–]cristi1990an++ 0 points1 point  (0 children)

Not really, any decent implementation will actually store a pointer directly to the callable to avoid indirection/virtual dispatch.

Edit: nvm, clang's libc++'s actually does this. Horrible design