MISRA Discussion Forums
A18-9-4 - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C++ (https://forum.misra.org.uk/forumdisplay.php?fid=18)
+--- Forum: AUTOSAR C++:2014 rules (https://forum.misra.org.uk/forumdisplay.php?fid=185)
+--- Thread: A18-9-4 (/showthread.php?tid=1714)



A18-9-4 - cgpzs - 15-10-2024

Hi!

I wonder if this code example technically violates A18-9-4?

Code:
#include <memory>

struct Foo
{
    explicit Foo(int);
};

int main()
{
    int val{};
    auto x = std::make_unique<Foo>(val);
    ++val; // non-compliant?
}

Since internally, std::make_unique takes the input arguments as "Args&&..." and std::forward's them into the constructor of the class.

Thanks!


RE: A18-9-4 - misra cpp - 22-10-2024

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
    }


RE: A18-9-4 - cgpzs - 23-10-2024

Understood, thank you for the response! It would be great to add a similar clarification to the equivalent rule 28.6.3 in MISRA C++:2023.