为什么将右值转换为左值引用不会引发警告?

Dan*_*ens 0 c++

有谁知道,为什么下面的代码没有发出警告?

struct Foo
{
    int a = 1;
};

struct Bar
{
    Foo getString()
    {
        return Foo();
    }
};

int main()
{
    Bar a;
    const Foo& b = a.getString();   <--- Foo getString() becomes Foo&?
}
Run Code Online (Sandbox Code Playgroud)

https://gcc.godbolt.org/z/GYzWa7

joh*_*ohn 5

没有什么可警告的。通过将const引用绑定到getString由其生命周期返回的临时对象,可以扩展以匹配引用的生命周期。