The following code violates AUTOSAR C++ rule 6-2-1:
Move and copy assignment operators shall either move or respectively copy base classes and data members of a class, without any side effects.
Move assignment performs actions other than moving data members and base classes.
Extra side effects might cause performance overhead and violate class invariant.
For detailed explanation of the rule, see here, page 114
PBufWrapper.h
struct PBufWrapper {
pbuf *firstElement = nullptr;
private:
DataSize_t m_freeSpace = 0; // (typedef uint16_t DataSize_t;)
}
PBufWrapper.cpp
PBufWrapper &PBufWrapper::operator=(PBufWrapper &&other) noexcept { // violates A 6-2-1
copySimpleMembersAndResetBuffer(other); // Found extra side effect!
if (other.firstElement != nullptr) {
firstElement = other.firstElement;
other.firstElement = nullptr;
}
return *this;
}
void PBufWrapper::copySimpleMembersAndResetBuffer(const PBufWrapper &other) {
m_freeSpace = other.m_freeSpace;
if (firstElement != nullptr) {
pbuf_free(firstElement);
firstElement = nullptr;
}
}
For pbuf_free see: here
I tried two things
- Test for Self-Assignment in
operator=()
:if (&other != *this) { ... }
std::swap(*this, other);
inoperator=()
These were not the solutions.
I would be very happy if someone can explain me how to solve it.
Edit
Related to the comment by "n. 1.8e9-where's-my-share m", I would like to know how I can overload std::swap and do an elementwise swap in this case?