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

Errors in transforming "int& a = A()" #593

Closed
leo-934 opened this issue Oct 15, 2023 · 1 comment
Closed

Errors in transforming "int& a = A()" #593

leo-934 opened this issue Oct 15, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@leo-934
Copy link

leo-934 commented Oct 15, 2023

#include <cstdio>
#include <utility>
struct A { operator int&(); };
 
int& ir = A();
int main()
{
  	return 0;
}

This block of code will be transformed to

#include <cstdio>
#include <utility>
struct A
{
  operator int & ();
  
};


 
int & ir = static_cast<int>(A().operator int &());

int main()
{
  return 0;
}

which can not be compiled correctly, because the return value of static_cast<int> is a rvalue and c++ standard does not allowed that non-const lvalue reference of type'int' binds to a rvalue of type int.

According to the standard, it should use the conversion function turnsA() into a int& type.

Here is the link of this term in a html version of cpp working paper.
https://timsong-cpp.github.io/cppwp/n4868/dcl.init.ref#5.2

@andreasfertig
Copy link
Owner

Hello @vinci-897,

thanks for raising that issue! It looks like showing ImplicitCastExpr for CastKind::CK_UserDefinedConversion isn't helpful because this introduces the invalid cast to int instead of int&. C++ Insights will no longer show that implicit cast, which makes the transformation correct and shorter in various cases.

Andreas

@andreasfertig andreasfertig added the bug Something isn't working label Oct 19, 2023
andreasfertig added a commit that referenced this issue Oct 19, 2023
Fixed #593: Don't show casts for user defined conversions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants