mercredi 22 juillet 2015

Moving out of one side of a ternary operator

I was writing some code similar to:

printer foo(bool b, const printer& fst, printer&& snd) {
  return b ? fst : std::move(snd);
}

and clang and copied snd out while gcc moved it out. I tried to minimize the example and I came up with:

#include <iostream>
#include <utility>

struct printer {
  printer() { }
  printer(const printer&) { std::cout << "copy" << std::endl; }
  printer(printer&&) { std::cout << "move" << std::endl; }
  printer(const printer&&) { std::cout << "const rvalue ref" << std::endl; }
};

int main() {
  const printer fst;
  printer snd;
  false ? fst : std::move(snd);
}

gcc 5.2 outputs

move

clang 3.6 outputs

const rvalue ref

Does the standard allow both the gcc and clang behavior?

gcc 5.2 disassembly

clang 3.6 disassembly

Aucun commentaire:

Enregistrer un commentaire