将临时对象转换为非常量引用时出错

问题描述 投票:4回答:2

这是一个有关using temporary stringstream object的问题的可复制示例:

#include <sstream>                     
#include <string>
#include <iostream>

using namespace std;

std::string transform(std::string);

int main()
{
    int i{};
    cout << transform( static_cast<stringstream &>(stringstream() << i).str() );
}

在MacOS High Sierra下尝试使用clang版本9.0.0]进行编译时,出现以下错误:

$ clang++ -std=c++11 x.cc -c
x.cc:12:24: error: non-const lvalue reference to type 'basic_stringstream<...>' cannot bind to a temporary of type 'basic_stringstream<...>'
    cout << transform( static_cast<stringstream &>(stringstream() << i).str() );
                       ^                           ~~~~~~~~~~~~~~~~~~~
1 error generated.

当在同一台机器上(以及Linux上)使用g ++ 9.2.0时,一切都可以正常编译。

似乎从stringstream &更改为const stringstream &stringstream &&可以解决问题。

问题是这是编译器错误还是clang对某些标准规则更严格?

这里是一个有关使用临时stringstream对象的问题的可复制示例:#include #include #include 使用...

c++ clang++
2个回答
0
投票

答案是libstdc ++和libc ++之间的ostream插入的不同实现。


0
投票

是,我认为这是libc ++中的错误。

© www.soinside.com 2019 - 2024. All rights reserved.