[英]error: cannot bind ‘std::basic_ostream<char>’ lvalue to ‘std::basic_ostream<char>&&’
[英]error: cannot bind ‘std::basic_ostream’ lvalue to ‘std::basic_ostream&&’ sl << ss;
我的朋友給我發了一個代碼,他說在 Windows 中成功編譯。 我在 linux 上進行了嘗試,但未能給出以下錯誤。 下面是代碼的最小可驗證示例。
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
std::stringstream ss, sl;
sl << ss;
}
但它給了
error: cannot bind ‘std::basic_ostream’ lvalue to ‘std::basic_ostream&&’
sl << ss;
為什么它在 windows 中有效,但在 linux 中無效,為什么會發生此錯誤?
自 C++11 以來,此代碼無法編譯,因為operator<<
沒有匹配的重載與std::stringstream
類型的兩個操作數。
但是,在 C++11 之前, std::ostream
提供了到void *
的隱式轉換,因此可以調用以下重載:
basic_ostream& operator<<( const void* value );
如果 stream 有錯誤,則 output 將與輸出 null 指針相同,否則一些未指定的非空指針值。
可能您的朋友使用了舊的編譯器,或者以舊的兼容模式運行的編譯器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.