5-2-12 - Does the rule apply for braced-init-list arg to initializer_list parameter? - 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.5 Expressions (C++) (https://forum.misra.org.uk/forumdisplay.php?fid=134) +---- Thread: 5-2-12 - Does the rule apply for braced-init-list arg to initializer_list parameter? (/showthread.php?tid=1591) |
5-2-12 - Does the rule apply for braced-init-list arg to initializer_list parameter? - DavidFriberg - 24-11-2021 Hi, Rule 5-2-12 covers array to pointer decay in a function call: Quote:Rule 5-2-12 (required, implementation, automated) An identifier with array type passed as a function argument shall not decay to a pointer. This rule has been brought over to the AUTOSAR C++14 guidelines in verbatim (as M5-2-12), where it applies to more modern C++. Now, an object of type std::initializer_list<E> (for some type E) is, as per [dcl.init.list]/5 (N4140/C++14), defined to be constructible from a braced-init-list as follows: Quote:An object of type std::initializer_list<E> is constructed from an initializer list as if the implementation allocated a temporary array of N elements of type const E, where N is the number of elements in the initializer list. Each element of that array is copy-initialized with the corresponding element of the initializer list, and the std::initializer_list<E> object is constructed to refer to that array. Some compilers (correctly within their implementation freedom) implements the constructor of their std::initializer_list class template with two arguments: a begin iterator for the "as if an array" argument, and a size of the list (automagically provided). Our static analyzer for AUTOSAR C++14 (whilst noting that this is a pre-C++11 rule) uses this to argue that any occasion where a braced-init-list is used as argument to a (compatible) function parameter that is a specialization of std::initializer_list is a violation of M5-2-12, essentially meaning we can neither use any of the std::initializer_list constructors from our in-house container classes, nor act as clients for many of the standard library's container classes (say under the assumption that we have an certified subset STL available): Code: #include <initializer_list> Is the intent that Rule M5-2-12 (in the modern C++ world) should prohibit any client side (braced-init-list argument) usage of std::initializer_list<E> function parameters? Thanks! RE: 5-2-12 - Does the rule apply for braced-init-list arg to initializer_list parameter? - misra cpp - 26-01-2022 When the MISRA rule was written initializer_lists had not been defined, so were not considered by the rule. It was adopted by Autosar unmodified. The view of the committee is that use of initializer_lists should be compliant with this rule, even if it can be assumed that it is implemented with an array decay. |