簡體   English   中英

嘗試投射std :: vector時遇到VS編譯器警告C4239 <std::pair<T, U> &gt;至其左值參考

[英]Got VS compiler warning C4239 when trying to cast std::vector<std::pair<T, U>> to its lvalue ref

我有一個Foo類,其構造函數編寫為

Foo::Foo(std::vector<std::pair<int, char>> &Data)
    : //Initialization list
{
    //Some other initialization
}

我試圖用在我的代碼中調用它

Foo(std::vector<std::pair<int, char>>
    {
        {10, 'a'}
    });

然后編譯器給我一個C4239,說

nonstandard extension used: 'argument': conversion from 
'std::vector<std::pair<int,char>,std::allocator<_Ty>>' to 
'std::vector<std::pair<int,char>,std::allocator<_Ty>> &'

我理解該消息,但是為什么編譯器對此轉換感到不滿意?

提前致謝。

您正在嘗試將臨時綁定到非常量引用。

考慮:

Foo::Foo(const std::vector<std::pair<int, char>> &Data)

根據Bjarne Stroustrup的C ++ 11-新的ISO C ++標准 (強調我):

在C ++中,非常量引用可以綁定到左值,而常量引用可以綁定到左值或右值,但是沒有什么可以綁定到非常量右值。 這是為了防止人們在使用新值之前更改被破壞的臨時值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM