mardi 18 septembre 2018

Shall we move to new way of self-assignment protection?

All

traditionally, in books on C++ and even in Core Guidelines the self-assignment protection is written as

A& A::operator=(const A& a) {
    if (&a != this) {
       ...
    }
    return *this;
}

But in modern C++ (since C++-11) we have std::addressof magic.

If I to teach students all the goodies of modern C++, shall I promote self-assignment check to be written as

A& A::operator=(const A& a) {
    if (std::addressof(a) != this) {
       ...
    }
    return *this;
}

More general question - should it be the way to go, in Core Guidelines and elsewhere?

Thoughts? Comments?

Aucun commentaire:

Enregistrer un commentaire