Skip to content

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

@leo-934

Description

@leo-934
#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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions