samedi 2 juillet 2016

What does static_cast<T> do to a T&?


So I asked this question and I was tinkering around with solving it via static_cast. (Incidentally it does solve the problem, I'm just not sure if I understand why.)

In the code:

vector<int> foo = {0, 42, 0, 42, 0, 42};
replace(begin(foo), end(foo), static_cast<int>(foo.front()), 13);

Is the static_cast simply constructing an R-Value int? What's the difference between that and just the call:

replace(begin(foo), end(foo), int{foo.front()}, 13);

EDIT:

As inferred by the answers static_cast does seem to construct an R-Value int: http://ideone.com/dVPIhD

But this code does not work on Visual Studio 2015. Is this a compiler bug? Test here: http://webcompiler.cloudapp.net/


Aucun commentaire:

Enregistrer un commentaire