mardi 6 juillet 2021

Is it allowed to self-move an object in C++? [duplicate]

Is it permitted for an object (in particular of an std class) to be self-moved or it is an undefined-behavior?

Consider an example:

#include <iostream>
#include <string>

int main()
{
    std::string s("123");
    s = std::move(s);
    std::cout << s;
}

In gcc/clang the program prints nothing, so s string is lost during moving: https://gcc.godbolt.org/z/xqTWKfMxM

But in MSVC it works fine.

Aucun commentaire:

Enregistrer un commentaire