Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std::move redundant?? #601

Closed
amutamil opened this issue Jan 1, 2024 · 5 comments
Closed

std::move redundant?? #601

amutamil opened this issue Jan 1, 2024 · 5 comments

Comments

@amutamil
Copy link

amutamil commented Jan 1, 2024

test t2 =std::move(t); is compiled to test t2 = test(static_cast<const test &&>(std::move(t)));
in which

std::move

isn't it redundant in compiled version ?
so compiled version should be
test t2 = test(static_cast<const test &&>(t)); ?

@andreasfertig
Copy link
Owner

Hello @amutamil,

thanks for your feedback!

Without a minimal working example, this is tough to say. In a quick example, I drafted (https://cppinsights.io/s/f4c0dc68), the transformation works as expected.

I'm happy to look at your example.

Andreas

@amutamil
Copy link
Author

amutamil commented Jan 9, 2024

Hello @andreasfertig ,

please refer this example https://cppinsights.io/s/647ccf1d,

Regards,
Amutamil E

@andreasfertig
Copy link
Owner

Hello @amutamil,

thanks for sharing the code.

Please have a close look at our two different versions. My struct test comes without a user-provided destructor. As a result, you can see that test, in my case, uses a compiler-provided move constructor (line 5 in the transformed output). Due to the destructor's presence in your example, only a compiler-provided copy constructor exists. This is why there is a cast around the std::move, which adds const.

In short, C++ Insights does what it is supposed to do: show you what's actually happening.

I wrote about this in one of my blog posts: andreasfertig.blog/2023/11/why-you-shouldnt-provide-an-empty-destructor/.

Andreas

@amutamil
Copy link
Author

Hello @andreasfertig,

Sorry if I deviated you from the real bug. I saw that video, from that video only I found this bug,

the real question is after C++ insights translate move into casting also why do we have std::move still being part of the translated version

test(static_cast<const test &&>(std::move(t))); here in last still why we have std::move(t). This can't be just test(static_cast<const test &&>(t));?

Amutamil E

@andreasfertig
Copy link
Owner

Hello @amutamil,

the real question is after C++ insights translate move into casting also why do we have std::move still being part of the translated version

C++ Insights doesn't translate move. I leave STL functions as they are. One reason why I'm not keen to add special code for std::variant. Whatever these functions internally do is part of their implementation.

test(static_cast<const test &&>(std::move(t))); here in last still why we have std::move(t). This can't be just test(static_cast<const test &&>(t));?

As I said above, std::move is not transformed. The cast you see is there because your code is not doing what you expect it to do. The cast C++ Insights shows you is adding const to the rvalue reference the inner std::move returns. If C++ Insights would transform move to a cast, your code would look like this:

test(static_cast<const test &&>(static_cast<test&&>(t)));

Andreas

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants