Skip to content

Incorrect constructor in return statement #351

@adromanov

Description

@adromanov

Consider the following code:


struct A {};

struct Test
{
    Test(const A &);
    Test(A &&);
};

std::optional<Test> foo()
{
    A a;
    return a;
}

in return statement constructor from rvalue reference is called (https://gcc.godbolt.org/z/KeWb4e). This is due to https://en.cppreference.com/w/cpp/language/return "Automatic move from local variables and parameters" part. This also could be seen in AST.
However, the output is (for the foo()):

std::optional<Test> foo()
{
  A a = A();
  return std::optional<Test>(a);
}

Here lvalue refeference constructor is called.

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