我正在尝试使用匿名ostringstream
来生成string
:Use an Anonymous Stringstream to Construct a String
但是,当我使用操纵器时,我似乎无法再编译:
const auto myString(static_cast<ostringstream>(ostringstream{} << setfill('!') << setw(13) << "lorem ipsum").str());
但即使在 gcc 5.1 中似乎也不允许这样做:
prog.cpp: In function
int main()
:
prog.cpp:8:109: error: no matching function for call tostd::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: 注意:候选
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: 注意:没有已知的参数 1 从std::basic_ostream<char>
到std::basic_ostringstream<char>&&
/usr/include/c++/5/sstream:565:7 的转换:注意:候选:
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: 注意:没有已知的参数 1 从std::basic_ostream<char>
到const __string_type& {aka const std::basic_string<char>&}
/usr/include/c++/5/sstream:547:7 的转换:注意:候选:
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: 注意:没有已知的参数 1 从std::basic_ostream<char>
到的转换std::ios_base::openmode {aka std::_Ios_Openmode}
这是另一个 gcc 流错误,还是我所做的实际上是非法的?