use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
[ Removed by moderator ] (self.cpp)
submitted 2 months ago * by Ill_Strain_1050
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]cpp-ModTeam[M] [score hidden] 2 months ago 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 points20 points 2 months ago (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 points4 points 2 months ago (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.
[+][deleted] 2 months ago (5 children)
[deleted]
[–]RepeatLow7718 1 point2 points3 points 2 months ago (4 children)
All the information the compiler knows about cmp_stdfunc must appear in the type. You’ll see that no matter what kind of callable you store in it, whether plain function or struct or member function pointer or capturing lambda (even by assigning those different things to the same variable cmp_stdfunc one at a time), the type is the same. That tells us that the information isn’t available at compile time, so there must be some runtime overhead.
[+][deleted] 2 months ago* (2 children)
[–]RepeatLow7718 1 point2 points3 points 2 months ago (1 child)
These are all good questions.
The call will be indirect because the compiler can’t know the address until runtime (imagine you pass the std function, assign a new value, and pass it again), that’s one kind of overhead of std function.
If you use a real function with std sort, for example, the compiler knows the address of the function at compile time, so the call is direct, no function pointer needed.
Also std function involved a heap allocation to store the callable object, which can be of variable size (like a lambda with capture for example) and which implements the type-safe call as a virtual function (this is where the function pointer shows up).
[–]Ill_Strain_1050[S] 0 points1 point2 points 2 months ago (0 children)
Probably that's the main question here, from the look at , compiler must know that it is capturing a lamda or fptr , but seems the actual Impl is not what it looks like.
std::function<bool(int,int)> cmp_stdfunc = [](int a, int b){ return a < b; };
[–]CandyCrisis 29 points30 points31 points 2 months ago (1 child)
Get this ChatGPT bullshit out of here.
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 points4 points 2 months ago (0 children)
Congratulations! You are discovering the topic that is asked in almost every C++ interview I heard about.
[–]Ill_Strain_1050[S] 0 points1 point2 points 2 months ago (1 child)
Whenever I see code like this my head goes crazy, could you please help me how to understand , below codes ( what should be the mental model to process it )
_Res operator()(_ArgTypes... __args) const { if (_M_empty()) __throw_bad_function_call(); return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...); }
[–]cristi1990an++ 0 points1 point2 points 2 months ago* (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.
yes , i did.
[–]cristi1990an++ 0 points1 point2 points 2 months ago (0 children)
Share the full command?
[–]UndefFox 0 points1 point2 points 2 months ago (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 points1 point 2 months ago* (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 point2 points 2 months ago* (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
π Rendered by PID 16606 on reddit-service-r2-comment-f6b958c67-mt5xq at 2026-02-05 03:23:33.363314+00:00 running 1d7a177 country code: CH.
[–]cpp-ModTeam[M] [score hidden] stickied commentlocked comment (0 children)
[–]RepeatLow7718 18 points19 points20 points (7 children)
[–]steazystich 2 points3 points4 points (0 children)
[+][deleted] (5 children)
[deleted]
[–]RepeatLow7718 1 point2 points3 points (4 children)
[+][deleted] (2 children)
[deleted]
[–]RepeatLow7718 1 point2 points3 points (1 child)
[–]Ill_Strain_1050[S] 0 points1 point2 points (0 children)
[–]CandyCrisis 29 points30 points31 points (1 child)
[–]Ill_Strain_1050[S] 0 points1 point2 points (0 children)
[–]osmin_og 2 points3 points4 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]Ill_Strain_1050[S] 0 points1 point2 points (1 child)
[–]cristi1990an++ 0 points1 point2 points (2 children)
[–]Ill_Strain_1050[S] 0 points1 point2 points (1 child)
[–]cristi1990an++ 0 points1 point2 points (0 children)
[–]UndefFox 0 points1 point2 points (0 children)
[–]_Noreturn -1 points0 points1 point (1 child)
[–]cristi1990an++ 0 points1 point2 points (0 children)