22-10-2024, 02:18 PM
The short (and unhelpful) answer is that the rule only talks about uses of std::foward, and this doesn't appear in your example, so as far as the example is concerned, the rule doesn't apply. Its a general principle that rules don't consider how library functions may be implemented.
We believe that the intent was to limit use after std::foward 'within the same function body, not in general'. So, even if there is a std::forward inside std::make_unique, your example is still OK (apart from the fact it doesn't compile - Foo needs a copy constructor).
The following example may clarify further:
#include <utility>
template <typename T> void foo (T && arg)
{
auto j = std::forward<T> (arg);
// non-compliant to use 'arg' here
}
void bar ()
{
int i = 0;
foo (i);
++i; // Compliant
foo (std::move(i));
++i; // Rule A18-9-4 doesn't apply, but
// non-compliant with A12-8-3 because
// of read of moved from object
}
We believe that the intent was to limit use after std::foward 'within the same function body, not in general'. So, even if there is a std::forward inside std::make_unique, your example is still OK (apart from the fact it doesn't compile - Foo needs a copy constructor).
The following example may clarify further:
#include <utility>
template <typename T> void foo (T && arg)
{
auto j = std::forward<T> (arg);
// non-compliant to use 'arg' here
}
void bar ()
{
int i = 0;
foo (i);
++i; // Compliant
foo (std::move(i));
++i; // Rule A18-9-4 doesn't apply, but
// non-compliant with A12-8-3 because
// of read of moved from object
}
Posted by and on behalf of
the MISRA C++ Working Group
the MISRA C++ Working Group