Wish people would stop using that term as a catch all for any and all legacy cruft in C++.
There's another thread where someone wished we could "break ABI" in order to change the iteration model for all std containers. Umm, pretty sure it'd be a bit more involved than that...
Seriously, this article makes an honest case for breaking ABI and properly paints it as the sacred cow it is. Instead of fixing the problem at its core, there are 5 conditional utilities put in place to address the original issue in various ways.
Should std::function be marked as deprecated then and removed in the future similar to auto_ptr? This is similar to fixes like lock and scope guard where issues are found but can't really be fixed without a new type to surplant it.
29
u/MFHavaWG21|🇦🇹 NB|P3049|P3625|P3729|P3786|P3813|P4216May 20 '26edited May 20 '26
Should std::function be marked as deprecated then and removed in the future similar to auto_ptr?
IMHO yes. I had a paper for that in C++26 that got no consensus - I will bring a new revision for C++29 (there was interest in deprecation post C++26).
No! It works fine, and I have no desire to stick std::super_long_function_name_but_wait_we_can_make_it_even_longer_copyable_moveable_exceptional_plus_plus_plus_function all over the place!
Auto_ptr was fatally broken. Std::function is not, please leave it alone.
Is this level of hyperbole really appropriate? It's just one word added, copyable, and I'm not aware of any place where the std has done this twice to the same type.
(I appreciate your point though. std::function's semantics, while in some ways undesirable, aren't what I would call broken.)
Well, if we were to deprecate it anyway, then why not just go ahead and break the ABI (mind you, I'm not seeing Sandor's claim yet that fixing std::function to be const-correct would break the ABI).
Exactly, because the code no longer compiles - because it's an API break that renders previously "perfectly valid" code ill-formed -, regardless of whether there would actually be an ABI impact of the change!
The question is I guess, when is it an ABI break to change the signature,
For the third time in this comment thread alone: adding support for const in the template signature of function and applying the design of move_only_function would not have been a hard ABI break (implementations could easily patch this up behind the scenes).
I don't get why old std::function is defect. IMO std::function is a wrapper for function pointer and a const pointer can point to something no-const. What do I miss?
The linked paper addresses this argument on bottom of page two by saying shallow const should be for reference types, and std::function isn't a reference type.
caling unique_ptr a "reference type" makes no sense to me tbh
from paper:
Their operations access and manipulate their means of referring to the referenced object, but do not access the referenced object itself . For example, none of unique_ptr<T>’s member functions accesses the T value; only the underlying T pointer.
given a T* ptr , would you really say ptr->~T() doesnt access the value? i wouldnt tbh
to clarify, i am not saying i disagree with conclusions of the paper (nor am i saying i agree), i am saying i dislike terminology used
A function is a reference to some code, it has to be a reference type. std::function is a wrapper for a bunch of data + code, but primarily it's the code, so I would call it a (crappy) reference type
Don't forget constant_wrapper. It's also callable and can be used to pass a function pointer while avoiding the cost of indirect call.
int foo(auto f) {
auto x = f();
// do something with x
return x;
}
int f();
int x = foo(f); // will call `f` via function pointer
int y = foo(std::cw<f>); // will directly call `f`
It was said before. New function types are verbose to type and put "function" awkwardly to the end. With such a bad ergonomics the adoption will be limited.
auto completion would work better if the "function" is a prefix not suffix that was the point.
I can make aliases but it's not what average c++ programmer does. You would expect the defaults are right for most programmers. But in C++ defaults are wrong.
I think alias definition should one of the earlier things somebody should learn... Its extremly easy and you can homogenise your code base (and especially give the standard library types better names)
130
u/Nicksaurus May 20 '26
All rise for the C++ national anthem
49
u/thehutch17 May 20 '26
Should std::function be marked as deprecated then and removed in the future similar to auto_ptr? This is similar to fixes like lock and scope guard where issues are found but can't really be fixed without a new type to surplant it.
6
u/VoodaGod May 20 '26
article complains that
const std::function<void()> f = Counter{};
f(); // OK (!)
is a defect but never shows if
const std::copyable_function<void()> f = Counter{};
f(); // Not OK (!)
3
u/_TheDust_ May 21 '26
I wonder if this could have been solved with a type like
std::generic_function<…, std::is_copyable + std::is_noexcept + std::is_reference>2
u/UnusualPace679 May 21 '26
Don't forget
constant_wrapper. It's also callable and can be used to pass a function pointer while avoiding the cost of indirect call.5
u/tpecholt May 20 '26
It was said before. New function types are verbose to type and put "function" awkwardly to the end. With such a bad ergonomics the adoption will be limited.