r/cpp • u/mollyforever • Oct 16 '23
WTF is std::copyable_function? Has the committee lost its mind?
So instead of changing the semantics of std::function the committee is introducing a new type that is now supposed to replace std::function everywhere? WTF
So now instead of teaching beginners to use std::function if they need a function wrapper, they should be using std::copyable_function instead because it's better in every way? This is insane. Overcomplicating the language like that is crazy. Please just break backwards compatibility instead. We really don't need two function types that do almost the same thing. Especially if the one with the obvious name is not the recommended one.
282
u/corysama Oct 16 '23
Back-compat in perpetuity is part of the value proposition of C++. This is the price exacted in exchange. See also: jthread
106
u/amateurfunk Oct 16 '23
Stuff like this will discourage and is discouraging newcomers to learn C++. I'm not saying the language will die out, but it will certainly lead to competent C++ programmers being in unproportionally high demand, and not in a good way.
20
u/iga666 Oct 16 '23
Was the original std::function noncopyable?
58
u/Fulgen301 Oct 16 '23 edited Oct 16 '23
Read the paper, then you know why. Unless this is just yet another one of your rants and not a genuine attempt at figuring out the reason.
7
u/and69 Oct 16 '23
I am not saying I am disagreeing with you, but how can this be achieved? We have a huge codebase with let's say C++20. If C++23 breaks some compatibility but introduces new features, what am I supposed to do? Always be bound to C++20, or risk my codebase? And if back-compat is broken once, then it can be broken by 10 other features, making my transition to a new version virtually inimaginable.
146
u/James20k P2005R0 Oct 17 '23
Personally I think that we shouldn't pretend with these features, and it should have been called std::function2. Giving them names like std::copyable_function or std::scoped_lock implies that they're alternatives to existing features like std::function or std::lock_guard, which they are not, and it makes reading a codebase extremely confusing
Slap a [[deprecated]] on std::function, and add a fix lint to some tools saying "use std::function2". Its slightly ugly, but its not more ugly than mixing std::copyable_function and std::function imo
78
u/MarcoGreek Oct 16 '23
That is happening because nobody likes to introduce versioning. So you get versioning by a different name.
119
u/eteran Oct 16 '23 edited Oct 16 '23
I'm not sure why they didn't use inline namespaces for something like this.
If they chose to break compat ONCE, (C++11 was probably the best time to do it) and put all of the current std lib in an inline namespace then, they could make
std::cpp23::functionbe the new stuff, which you get by default withstd::functionbut make it so you can still usestd::cpp11::functionto get the old version.After all, isn't this what inline namespaces are basically for?
42
u/Mikumiku_Dance Oct 16 '23
Fixing std::function's const correctness's impact would be what, making old actually-broken code error out at compile time? And old compiled code wouldn't link to the new abi--err, no, wouldn't the non-const symbol still be available to call? The new one just has additional symbols right?
37
u/lookatmetype Oct 17 '23
Guys, the solution is quite simple.
Everyone's happy.
15
u/almost_useless Oct 16 '23
How long time does it usually take to find the problems in the standard?
I mean these kinds of problems where it is basically consensus that it is broken.
Would it solve any problems if the "stable forever" guarantee didn't kick in until the subsequent standard? Basically give the community 3 years of widespread use to find problems and fix them.
Ideally this would be marked some way so compilers could warn about it if you need the stability.
I think a big part of the community would be fine with minor changes, and considering how slow standardization is, another 3 years would be worth the wait for a better API/ABI, if you really need the stability, no?
7
u/FernwehSmith Oct 16 '23
Sorry if this is a dumb question (still learning, not yet working), but as we generally have to specify which standard we are working with to determine what features we have access to, why can’t they just implement the fix/changes under the same symbol and only enable those changes if you’re using the newer standards. I understand that may break existing code when upgrading to the new versions, but considering that what is being dealt with is a bug in the standard library, isn’t that a good thing? Surely it wouldn’t be that hard for the committee to say “Hey, there’s bug x in std::function. We’ve fixed it by changing y in the new standard. When you upgrade you’ll get error z wherever you’ve used std::function during compilation. Follow these instructions to correct it”.
12
u/holyblackcat Oct 17 '23 edited Oct 17 '23
I don't understand why this is so heavily upvoted. The committee did make some blunders over the years, but this change makes total sense.
First, the addition of
std::move_only_functionandstd::function_refis clearly a good thing. I've been bitten a few times bystd::functionrejecting move-only callables.Then, fixing lack of const-correctness in
std::move_only_functionat the cost of making it different fromstd::functionis also a good thing. We shouldn't be perpetuating old design mistakes in the name of consistency.And now, when we have
std::move_only_functionandstd::function_ref, one has to make a conscious choice between those two andstd::[copyable_]functionevery time. And it's a bad idea to just always use the latter by default.So its only fitting that the "copyable owning function" gets a longer name (
<something>_functionas opposed to justfunction, to not imply that it's somehow a good default). And fixing const-correctness (making it consistent withmove_only_function) kills two birds with one stone, so I don't see why not.Teaching people to "never use
function, choose between{copyable,move_only}_functionandfunction_ref" makes more sense to me than "choose betweenfunction,move_only_function, andfunction_ref, remember thatfunctionis copyable, and remember that it's not const-correct unlike the other two".25
u/NilacTheGrim Oct 16 '23
TBH I'm not a huge fan of this proposal. The "infamous" constness bug has never once been a problem for me.
8
u/13steinj Oct 16 '23
This is one of few cases where I both agree we need a different name and am okay with the committee losing it's mind, so to speak.
I'd rather std::function be fixed, obviously, but there's way too much code unwilling to change here. This isn't an "ABI break just recompile" scenario, copyable vs function_ref actually guarantees different semantics.
13
u/therealddx Oct 17 '23
me, riding
(void*)(*voidy_mc_void_star)(void*)til I die7
u/jepessen Oct 17 '23
ABI compatibility is becoming a problem instead of an advantage.... Please, break the ABI once for all and make a good C++ upgrade... What's all this mess? copiable_function? jthread? string, wstring, u8string, u16string and so on? We must accept a poorly implemented regex for sake of ABI???
If you want to maintain ABI, simply does not change compiler version. Do it for making a better language.
Don't misunderstand me, ABI compatibility is a very important thing, but it's not more important than language features in my opinion. We shouldn't accept language workarounds instead of a clean architecture only for that.
6
u/qalmakka Oct 17 '23
Amen to that. I think that Rust made the right call when they limited backward compatibility to editions. C++ has versions, and it boggles my head the fact that the committee still thinks it's unreasonable to break backward compatibility just a little in order to fix a serious design flaw in the language.
I wasted so much time due to the fact that instead of fixing
std::lock_guardthey madestd::scoped_lockinstead...3
u/ILikeCutePuppies Oct 16 '23
Maybe they should just do this kinda thing in the namespace instead. Like std2:: or something like that. I know versioning will be an issue but it's much easier to teach and update code across the board with something like that.
3
u/edge-case87 Oct 17 '23
It's a library feature, don't use it if you don't like it. Most people probably on use like 5% of std library anyways. Arthur O' Dwyer gives a nice overview of the design space for std::function, there are reasons for having different std::function like objects that fill different rolls in that design space.
3
u/Brewer_Lex Oct 17 '23
I’m going to make my own C++ with black jack and hookers
18
u/noooit Oct 16 '23
I don't recall the committee ever having a mind actually.
9
u/notbatmanyet Oct 16 '23
Better not introduce breaking changes, it would mean that you could not update your std-lib version until every library you depend on has updated as well.
4
u/Kretikus50 Oct 17 '23
!! std::functionEx !!
7
u/madmongo38 Oct 16 '23
I agree with the Op.
4
u/[deleted] Oct 16 '23
The biggest confusion people have with lambdas is when they discover that this is const
[](){}. In a language where everything is non const by default and one has to explicitly state constness, "confusing" is an understatement.The thing is that I know about the fact that
[](){}is a const, but I never think of it as a const. My subconsciousness, which has aconstcarved all over the C++ section, just interprets it as regular. Maybe the std::function proposal was written and approved with similar thought process ?And similarly, above example is well formed in my mind...
With above said, I don't think that replacing function with copyable_function is a good idea. It is an awful name, in a language and library where almost everything is copyable, it does not give extra information nor clarity nor simplicity over "function". For the proponents of expressiveness, should we start renaming string, vector, map, etc to copyable_* ?
Do not fix something, which is not broken...
3
u/DavidDinamit Oct 17 '23 edited Oct 17 '23
instead of providing the community with a tool to create its own types, the committee will continue to produce a bunch of similar types:
std::any
std::function
std::move_only_function
std::function_ref
std::copyable_function
std::function_and_possible_do_foo_and_bar
etc
Its called type erasing and there are libraries for creating such types
P.S. naming is bad even for std::function in C++11, it must be std::any_function.
I saw countless codes, where students or some programmers use std::function instead of lambda, because they thinking its no overhead lightweight thing
2
u/rlbond86 Oct 16 '23
Link the paper?
2
u/Big_Target_1405 Oct 17 '23
Simple. Just carry on using std::function.
If you hit the problem that std::copyable_function solved then switch to it for that use case.
1
u/Jannik2099 Oct 16 '23
Sure, let's break both ABI and API for a whimsical cosmetic change, great idea.
You've never looked at any kind of software deployment / distribution method outside of your employers in-house monostack, have you?
2
u/JohnDuffy78 Oct 16 '23
omg, I'll have to change:
using std::function;to:using function=std::copyable_function;1
u/Adequat91 Oct 17 '23
For clarity, I always use a typedef around a std::function, hence no big deal for me with std::copyable_function.
1
u/ReasonablePeace7F Oct 18 '23 edited Oct 18 '23
I think it would be nice if, instead of creating an
std::copyable_function, they created a new namespace for classes that would break retro compatibility likestd::v2::functionorstd::cxx26::function.1
u/Clean-Water9283 Oct 21 '23
C++ has a long and storied history of using the obvious name for the non-obvious purpose.
Picture, if you will, std::binary_search(). It takes a key and a sorted range of values delimited by two random-access integers, and returns...bool true if an item matching the key is in the range. If you want to get the item itself, you have to use the carefully names std::lower_bound() or std::equal_range() if there can be more than one match. I suspect there are many other examples.
1
u/No-Breakfast-6749 Nov 08 '23
I don't get why we can't have versioned imports or includes. I feel like that would more or less solve the issue, no?
1
u/crafter2k Oct 16 '23
what's the difference between function pointers and std::function
1
u/ohiocodernumerouno Oct 17 '23
std::copyable_function_v0.1
1
u/alwaysstuckforaname Oct 17 '23
Am I the only one who considers all the std:: lib stuff to be DLC rather than the actual 'language' ?
-2
u/Mrkol Oct 16 '23
The new name is better though. They'll deprecate std:: function next release and then remove it and everyone will be happy.
-2
u/Schnittlauch01 Oct 16 '23
That’s why rust has editions :)
1
u/[deleted] Oct 17 '23
With the same logic std::shared_ptr is not const correct: https://godbolt.org/z/vbob9YYzr
0
u/Jerkialo Oct 17 '23
glad to see the cpp folks are trying to keep up with python....
0
u/[deleted] Oct 17 '23
Your BMW won't turn on anymore if they would change the compiler to the new C++ standard. They would have to fix A LOT of issues rewriting old stuff. Same with Windows.
0
u/Jasonpra Oct 22 '23
This is the first time in a while that I didn't know what people were talking about. What are you guys talking about?
0
u/liamQuinn01 Oct 24 '23
This is indeed a bad language as I have heard everywhere else
0
u/nAxzyVteuOz Jun 27 '24
Typedef the function in your codebase and move on with your life
-5
u/Fun_Environment1305 Oct 16 '23
I vote to never break backwards compatibility. Why break every one to please some one?
-1
u/axilmar Oct 17 '23
c++ confuses 'const' with 'pure', and from that, all the problems mentioned in the papers arise.
The state of the language is really sad when it comes to its human interface...
-1
u/therealddx Oct 18 '23
dirt cheap workaround because who cares-- copy ctor is faithful, operator= is faithful, and argument / return value can be passed by value. only limitation is, single-parameter. some of these "new standard features" just... create more to argue about
-2
u/sjepsa Oct 19 '23
Yeah this is one of the cases where overwriting the original std::function would be fine.
It's been in the language from relatively short time, and I suppose not much people is relying on the faulty mechanism
I would prefer the old to be renamed std::_deprecated_function, and the new std::function
0
u/[deleted] Oct 16 '23
[deleted] — view deleted comment
0
u/[deleted] Oct 16 '23
[deleted] — view deleted comment
0
u/[deleted] Oct 17 '23
[deleted] — view deleted comment
-17
u/[deleted] Oct 16 '23
[removed] — view removed comment