C++ temporary variable lifetime

后端 未结 3 920
清歌不尽
清歌不尽 2021-01-18 17:09

Is this code valid?

int foo()
{
    std::vector& v = std::vector(5, \"X\");

    // Do something silly...
         


        
3条回答
  •  北海茫月
    2021-01-18 18:13

    The code you've shown is illegal – temporaries can only bind to rvalue references or const lvalue references.

    VC++ happens to allow it as an extension (and gives a level 4 warning saying so).

提交回复
热议问题