MISRA Discussion Forums

Full Version: A18-9-4
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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!
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
    }
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.
We are currently reworking rule 28.6.3 for the next release