Why is there different elision behaviour between static_cast<T&&> and move<T> in VS 2017

Viewed 96

I am struggling to understand the different behavior shown between move and an rvalue cast

given:

template <class T> void f1(T param) {  }

struct B { B() = default;  B(B&&) { cout << "Move constructor\n"; } };
void demo()
{

    f1(static_cast<B&&>(B())); // does not call move constructor - constructor is elided
    f1(move(B())); // calls move constructor - no elision
}

The move call forces the call to the move constructor, while the static cast does not, and the constructor is apparently elided. This seems strange as the definition of std::move looks very much like a static cast.

Why the different behavior?

Update: Cannot be reproduced with gcc. Behavior shown only with MSVC

0 Answers
Related