I'm getting pushback related to changing to using a const reference instead of letting it do a copy.
The concern is that in a multi-threaded app that if we return a const reference to a collection, could something else change the collection and affect the const reference that was returned?
Basically, are there scenarios where we don't want to change to a const reference and if so, are all accounted for in this new check?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
If you just change the class there should be no behavior in the code at all since you should still store it in anon-reference and thus using a copy.
Unless you had the pattern that you define the local variable as a temporary const reference although it does return a copy. That is a pattern I would discourage though (would make sense to report this an issue - will file a ticket later if none exists yet).
If you are really planning to store references to data within objects that data should not be mutable within the object and the object instance you referencing should also not be mutable. That just seems like a cause for trouble to begin with.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
That looks like flawed code. If fred is const there should be no issue as this code would not compile. In case you are taking a reference from a mutable object you should not store a reference but a copy.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
how flawed do you think this is.. would you even think that a warning would be wanted if calling a method to a non-const object and storing the result in a const reference?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
About QString. If we add attribute view=true to the QString configuration I don't think Cppcheck will warn about it. I don't know off the top of my head for sure if that is proper.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The concern is that in a multi-threaded app that if we return a const reference to a collection, could something else change the collection and affect the const reference that was returned?
I think it would be interesting with a minimal example code where Cppcheck writes a false positive.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Example code that doesn't produce the warning but shows how things could change (basically your suggestion above). So the code isn't really flawed and does compile.
Certainly! When considering the new return on a reference check, it’s essential to assess how the insights align with the candidate’s experience. For instance, if a reference highlights the candidate’s work on outdoor projects, including their management of a balcony turf installation, it could provide valuable context about their practical skills and suitability for the role. Understanding these details helps ensure a more informed hiring decision.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I'm getting pushback related to changing to using a const reference instead of letting it do a copy.
The concern is that in a multi-threaded app that if we return a const reference to a collection, could something else change the collection and affect the const reference that was returned?
Basically, are there scenarios where we don't want to change to a const reference and if so, are all accounted for in this new check?
I understand the concern that's why the check was done to only report it in a very narrow case (see https://trac.cppcheck.net/ticket/12546).
If you just change the class there should be no behavior in the code at all since you should still store it in anon-reference and thus using a copy.
Unless you had the pattern that you define the local variable as a temporary const reference although it does return a copy. That is a pattern I would discourage though (would make sense to report this an issue - will file a ticket later if none exists yet).
If you are really planning to store references to data within objects that data should not be mutable within the object and the object instance you referencing should also not be mutable. That just seems like a cause for trouble to begin with.
hmm I think that is interesting feedback. I guess fixing this could break the code. Even in non-threaded cases.
For example, if a getter is called like this:
It's unfortunate to suggest that getX returns a const reference here.
That looks like flawed code. If
fred
isconst
there should be no issue as this code would not compile. In case you are taking a reference from a mutable object you should not store a reference but a copy.It's not very flawed imho. fred is not const obviously. The scenario is that it compiles.
Last edit: Daniel Marjamäki 2024-05-02
how flawed do you think this is.. would you even think that a warning would be wanted if calling a method to a non-const object and storing the result in a const reference?
About QString. If we add attribute
view=true
to the QString configuration I don't think Cppcheck will warn about it. I don't know off the top of my head for sure if that is proper.I think it would be interesting with a minimal example code where Cppcheck writes a false positive.
Example code that doesn't produce the warning but shows how things could change (basically your suggestion above). So the code isn't really flawed and does compile.
Last edit: Steve Albright 2024-05-02
Certainly! When considering the new return on a reference check, it’s essential to assess how the insights align with the candidate’s experience. For instance, if a reference highlights the candidate’s work on outdoor projects, including their management of a balcony turf installation, it could provide valuable context about their practical skills and suitability for the role. Understanding these details helps ensure a more informed hiring decision.