gpt4 book ai didi

c++ - 操纵器是否以某种方式转换流类型?

转载 作者:行者123 更新时间:2023-11-30 02:33:13 27 4
gpt4 key购买 nike

我正在尝试使用匿名 ostringstream生成 string : Use an Anonymous Stringstream to Construct a String

但是,当我使用操纵器时,我似乎无法再编译了:

const auto myString(static_cast<ostringstream>(ostringstream{} << setfill('!') << setw(13) << "lorem ipsum").str());

但这似乎是不允许的even in gcc 5.1 :

prog.cpp: In function int main():
prog.cpp:8:109: error: no matching function for call to std::basic_ostringstream<char>::basic_ostringstream(std::basic_ostream<char>&)
const auto myString(static_cast<ostringstream>(ostringstream{} << setfill('!') << setw(13) << "lorem ipsum").str());


In file included from /usr/include/c++/5/iomanip:45:0,
from prog.cpp:1:
/usr/include/c++/5/sstream:582:7: note: candidate
std::basic_ostringstream<_CharT, _Traits, _Alloc>::basic_ostringstream(std::basic_ostringstream<_CharT, _Traits, _Alloc>&&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]
basic_ostringstream(basic_ostringstream&& __rhs)

/usr/include/c++/5/sstream:582:7: note: no known conversion for argument 1 from std::basic_ostream<char> to std::basic_ostringstream<char>&&
/usr/include/c++/5/sstream:565:7: note: candidate:
std::basic_ostringstream<_CharT, _Traits, _Alloc>::basic_ostringstream(const __string_type&, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::basic_ostringstream<_CharT, _Traits, _Alloc>::__string_type = std::basic_string<char>; std::ios_base::openmode = std::_Ios_Openmode]
basic_ostringstream(const __string_type& __str,

/usr/include/c++/5/sstream:565:7: note: no known conversion for argument 1 from std::basic_ostream<char> to const __string_type& {aka const std::basic_string<char>&}
/usr/include/c++/5/sstream:547:7: note: candidate:
std::basic_ostringstream<_CharT, _Traits, _Alloc>::basic_ostringstream(std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::ios_base::openmode = std::_Ios_Openmode]
basic_ostringstream(ios_base::openmode __mode = ios_base::out)

/usr/include/c++/5/sstream:547:7: note: no known conversion for argument 1 from std::basic_ostream<char> to std::ios_base::openmode {aka std::_Ios_Openmode}

这是另一个 gcc 流错误,还是我所做的实际上是非法的?

最佳答案

static_cast<ostringstream>(...)

这将尝试从括号中的参数构造一个新的ostringstream,一个std::ostream&,它没有std::ostringstream 的构造函数| .

您只想将引用转换回原始类型。转换为引用:

static_cast<ostringstream&>(...)

然后它工作正常。

我不知道你认为什么有效,但省略了引用,并删除了操纵器,它仍然失败。

关于c++ - 操纵器是否以某种方式转换流类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35731123/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com