jeudi 26 avril 2018

Is it possible to reset reference to another value in C++?

I know generally it's impossible to reset a reference after it's already initialized.

However, I somehow try out the following code and it happens to work on both clang++ and g++.

My question is, is the following a valid (behavior-defined) C++?

std::string x = "x";
std::string y = "y";
std::string i = "i";
std::string j = "j";

// now references to x, y
std::pair<std::string &, std::string &> p { x, y };
// now references to i, j
new (&p) std::pair<std::string &, std::string &> {i, j};

Is it good C++? Thanks.

Aucun commentaire:

Enregistrer un commentaire