-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[inplace_function] Const-callability means thread-safety #158
Comments
For once we have the chance to choose the right default in I'd much rather go with |
@Voultapher You make some good points. But I continue to think that the biggest advantage of
True, if you want to pass an
The discouraged "return by const value" looks like this: |
While this is nice to have, there is neither a full convergance of interfaces nor should it be difficult to string replace existing signatures, so it shouldn't overrule all other arguments.
Commonly owning a type erased closure is the main usage, then again, looking through github it seems the vast majority of users 'misuse' In the past C++ has repeatedly made the mistake of making things unnecessarily expert friendly in an attempt at satisfying an incompatible unweighted soup of requirements. Let's please not repeat that. |
While I'm sympathetic to the fact
looks bad, this is the price you pay for type erasure (and simplicity -- we could arguably remember we swallowed a free function those don't have state to mutate). The question really is: what is the use case for a const version of your wrapper? But what does it mean to "view" a function -- this gets right back to whether calling a function should be able to mutate it's state. Which should take precedence if they conflict? At the end of the day
is not an "abuse" of the type system for free functions, because the template signature is a constraint on the wrapper class -- it's promising it won't call anything that would mutate its state, and free_function conforms to that contract. If the worst thing about your API is |
Right now,
inplace_function
permitsThat is, if I have a reference to a
const inplace_function
, I can call itsoperator()
, which can mutate its internal state, even though I've promised not to modify it!I believe we ought to follow the lead of Folly::Function, fu2::function, etc., and split
inplace_function
into two kinds of specialization:inplace_function<R(As...)>
is constructible from either const-callable or non-const-callable lambdas, and provides onlyoperator()
(non-const)inplace_function<R(As...) const>
is constructible only from const-callable lambdas, and provides onlyoperator() const
In this paradigm, you would not be permitted to write
The call to
f(1)
would fail to compile becauseinplace_function<int(int)>::operator()(int)
is not const-qualified. Instead, you would have to write eitheror
or
depending on the semantics you want.
The text was updated successfully, but these errors were encountered: