操纵器是否以某种方式转换流类型
Do Manipulators Convert the Stream Type Somehow?
>我正在尝试使用匿名ostringstream
生成string
: 使用匿名字符串流构造字符串
但是,当我使用操纵器时,我似乎无法再编译了:
const auto myString(static_cast<ostringstream>(ostringstream{} << setfill('!') << setw(13) << "lorem ipsum").str());
但即使在 gcc 5.1 中,这似乎也是不允许的:
prog.cpp: 在函数
int main()
中:
prog.cpp:8:109:错误:调用std::basic_ostringstream<char>::basic_ostringstream(std::basic_ostream<char>&)
没有匹配函数const auto myString(static_cast<ostringstream>(ostringstream{} << setfill('!') << setw(13) << "lorem ipsum").str());
在/usr/include/c++/5/iomanip:45:0 包含的文件中,
来自 Prog.cpp:1:
/usr/include/c++/5/sstream:582:7:注意:候选
std::basic_ostringstream<_CharT, _Traits, _Alloc>::basic_ostringstream(std::basic_ostringstream<_CharT, _Traits, _Alloc>&&)
[带_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)
[带_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)
[带_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 流错误,还是我正在做的事情实际上是非法的?
static_cast<ostringstream>(...)
这将尝试从参数中的参数构造一个新的ostringstream
,一个std::ostream&
,没有std::ostringstream
的构造函数。
您只想将引用转换回原始类型。将演员转换为参考:
static_cast<ostringstream&>(...)
然后它工作正常。
我不知道你认为什么有效,但是省略参考并移除机械手,它仍然失败。
- 以线程安全的方式转换 C/C++ 中时区名称字符串的时区偏移量
- 如何以滑动窗口方式从 std::bitset 读取位并将它们转换为 int?
- 在C++中以安全的方式将字符*转换为uint8_t*
- 如何强制 GCC 以线性方式转换易失性内联程序集语句
- 如何以优雅高效的方式将无符号/有符号整数/长整型转换为 C 字符串?
- Windows VC++编译器允许一种奇怪的c样式转换方式(或者它可能不是转换??)
- 我可以使用什么方法或方式将用户输入的字符串转换为 C++ 中的整数
- 如何将 boost::beast 中的序列化数据转换为字符串,以便我可以以 FIFO 方式处理它?
- 一种体面的方式来转换const列表参考参数,然后传递到另一个函数
- 类型转换C++的行为方式很奇怪
- 如何以优雅的方式将C++代码转换为C
- 将UTC tm*转换为本地化tm*的跨平台方式
- 是否可以以编程方式将 SQLite 数据库转换为 C/C++ 中的 SQL 语句
- 将CHAR转换为TCHAR的最快捷、最优雅的方式
- 如何以可移植的方式将integer转换为little-endian
- 在std::string和std::wstring之间转换的多平台方式
- 从抽象类向下转换到右侧模板的最佳方式
- 操纵器是否以某种方式转换流类型
- 最有效的方式转换数据帧到矩阵,反之亦然
- 在boost中,如何将boost迭代器传递给以某种方式转换为std::string的函数