-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Proposal for value_ref<T> alias template #363
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
Comments
It seems the opposite (e.g. |
I see your point. I think |
Are we sure we should start to name absolutely every specific use case we can find in our code? We could end up with a lot of cognitive overhead. I find reasonable for some cases, but maybe not for all. Just a thought. |
|
As I wrote in the OP, the point is to specify what the function cares about - does it need a value or an object? It's an important semantic distinction. The user would know from the function signature whether they need to care about lifetime issues or not. |
Sorry, I misread the original post, it is a good point. |
On 10/30/2015 8:09 AM, andreabrambilla wrote:
Note that we need to minimize annotations. If programmers must write
|
I think What you're trying to do is disambiguate between uses of a 1: A function that takes a non-modifiable reference to what is potentially a temporary object. 2: A function which is going to store a reference to an object, but in this case it just so happens to be a To me, the answer is simple. If you want 1, you use Also, most people who store such things will be storing them as pointers. So you may as well get the pointer ASAP and make it clear. Generally speaking, I would say that getting the address of a reference is a code smell. Obviously there are plenty of valid uses for it. But it is a bit dubious. |
👍 |
I think we should assume that more users will know the very common idiom const T& than will know an alias. Too many aliases can make code impenetrable. |
Concerning "F.20: Use a const T& parameter for a large object", I'd like to propose the following alias template:
This would document the fact that the function is concerned with the value of an object, rather than its address in memory - although its parameter binds directly to the argument rather than creating an independent copy of it.
This clarifies the semantics of the function without requiring inspection of its definition - for instance, it will be clear that the function won't store the address of the argument anywhere, that there won't be lifetime issues, and that the argument is taken by reference only to avoid pessimization.
In other words, I think it is important for a client to know whether a function needs a value or an object, and
T const&
is ambiguous about that. We could use the type system to provide semantic annotation, just asowner<T*>
does.Tools may - for instance - detect attempts to take the address of a value parameter and report it (if the function only cares about the value, taking the address could be a suspicious operation leading to lifetime issues). But even without support from tools, this will make it easier for the user to reason about a function.
I personally find this article very convincing, and although I am aware that it is somewhat in contrast with the current guidelines, I thought you might find it inspiring too.
The text was updated successfully, but these errors were encountered: