MISRA Discussion Forums
A8-4-5: Should have an exception for move constructors - 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: A8-4-5: Should have an exception for move constructors (/showthread.php?tid=1725)



A8-4-5: Should have an exception for move constructors - cgpzs - 05-02-2025

Our static analyzer is complaining about our move constructor because we are not moving the entire "oth" input. Instead, we are doing member-wise move, in compliance with A12-8-1:

Code:
class B
{
public:
  B(B&& oth) : ptr(std::move(oth.ptr)) // Compliant
  {
     oth.ptr = nullptr;
  }
};

Do you agree that A8-4-5 should explicitly clarify that the rule does not apply to move constructors?


RE: A8-4-5: Should have an exception for move constructors - misra cpp - 14-02-2025

Its a bit difficult to answer your question as B doesn't have any members. We assume it has a member 'sometype *ptr;' but does it have any other members?

If 'ptr' is the only member, then we'd say that the example is compliant with both A8-4-5 and A12-8-1.


RE: A8-4-5: Should have an exception for move constructors - cgpzs - 17-02-2025

Yes, in the example I took from AUTOSAR, B has only one member of type "std::int32_t* ptr;".

Let's assume it has 2 such members, ptr1 and ptr2, would this be compliant with A8-4-5?

Code:
class B
{
public:
  B(B&& oth) :
    ptr1(std::move(oth.ptr1)),
    ptr2(std::move(oth.ptr2))
  {
     oth.ptr1 = nullptr;
     oth.ptr2 = nullptr;
  }
private:
   std::int32_t* ptr1;
   std::int32_t* ptr2;
};



RE: A8-4-5: Should have an exception for move constructors - misra cpp - 21-02-2025

Short answer - yes, it would be compliant