Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A18-9-4
#1
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!
Reply
#2
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
    }
Posted by and on behalf of
the MISRA C++ Working Group
Reply
#3
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.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)