Rule 7-5-3 and storing a reference for later use - Printable Version +- MISRA Discussion Forums (https://forum.misra.org.uk) +-- Forum: MISRA C++ (https://forum.misra.org.uk/forumdisplay.php?fid=18) +--- Forum: MISRA C++:2008 rules (https://forum.misra.org.uk/forumdisplay.php?fid=19) +---- Forum: 6.7 Declarations (C++) (https://forum.misra.org.uk/forumdisplay.php?fid=136) +---- Thread: Rule 7-5-3 and storing a reference for later use (/showthread.php?tid=1421) |
Rule 7-5-3 and storing a reference for later use - rrosier - 08-05-2018 Hello, I am curious if Rule 7-5-3 is only applicable when a reference to a parameter that is passed by const reference is returned from a function, but also if that reference is stored and then used later. The rationale of the rule states that "It is implementation-defined behaviour whether the reference parameter is a temporary object or a reference to the parameter." and then goes on to say that a temporary object will be destroyed when the function returns and that using an object after its destruction will lead to undefined behaviour. Considering the following code: Code: class TestConstRef If I understand the rule correctly, the parameter aT could be a temporary object which will be destroyed when the constructor exits and when the member reference mT is used later, the temporary object that it refers to may (depending on the implementation) no longer exist and that will lead to undefined behaviour. Could I ask:
Re: Rule 7-5-3 and storing a reference for later use - dg1980 - 09-05-2018 Interesting point. I think the rule refers to chapter 5.2.2 paragraph 5 of ISO/IEC 14882:2003: Quote:Where a parameter is of const reference type a temporary object is introduced if However, you can make the same mistake by storing a reference to a temporary object: Code: class cFoo Re: Rule 7-5-3 and storing a reference for later use - rrosier - 09-05-2018 I am also concerned about the following type of snippet: Code: #include The line where the temporary int32_t is created is perfectly legal code, but the reference to it will be lost when the constructor exits. However, the compiler will not warn you whether a temporary is created or not. So, unless you have some other way to ensure absolutely that a temporary is not created for this sort of example, you could end up with a reference to a temporary. Furthermore, the MISRA rule seems to suggest that temporaries can be introduced on an implementation-defined basis. This seems to be borne out by the chapter 5.2.2 paragraph 5 of ISO/IEC 14882:2003 mentioned - which states that a temporary object is introduced "if needed". It does not specify what "if needed" means - so could this not vary system-by-system, such that even if you inspect the code and decide for yourself that temporaries are not needed, the compiler might decide otherwise? Re: Rule 7-5-3 and storing a reference for later use - rrosier - 09-05-2018 Sorry, this was a double-post of the previous post - as the system gave me an error message - but I can't seem to find a way to delete it. Re: Rule 7-5-3 and storing a reference for later use - misra cpp - 17-07-2018 We agree with your interpretation of 7-5-3, and we recognise the limted scope of the current rule We are currently working on a new version of the standard, and the aim for this rule is that we move toward the requirements of MISRA C:2012 18.6 'The address of an object ... shall not be copied to another object that persists after the first object has ceased to exist' |