将C弦发送到非效果STD :: OSTRINGSTREAM怪异行为
sending c-string to non-lvalue std::ostringstream weird behaviour
本文关键字:OSTRINGSTREAM STD 更新时间:2023-10-16
测试代码:
#include <iostream>
#include <sstream>
int main() {
std::ostringstream q;
std::cout << (dynamic_cast<std::ostringstream&>(q<<"hello"<<101).str()) << "n";
std::cout << (dynamic_cast<std::ostringstream&>(std::ostringstream()<<"hello"<<101).str()) << "n";
return 0;
}
用:g test.cpp编译输出:
hello101
hello101
编译:G -std = C 98 Test.cpp输出:
hello101
0x4b2ec0101
看起来第二个字符串包含指向字符串本身的" Hello"字符串的指针。为什么?是GCC中的C 98标准或错误的某些"功能"?
在C 03中,非成员operator<<
(源(,负责打印C字符串,即
template< class Traits >
basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os,
const char* s );
无法接受rvalue流,因此选择了成员过载(从std::ostream
基类继承((来源(:
basic_ostream& operator<<( const void* value );
这打印出地址。
在C 11中,有一个RVALUE流插入操作员,
template< class CharT, class Traits, class T >
basic_ostream< CharT, Traits >& operator<<( basic_ostream<CharT,Traits>&& os,
const T& value );
确保LVALUE和RVALUE流的行为将相同。请注意,此过载不可能写在C 03中,因为绑定到RVALUE的唯一方法是通过const lvalue参考。
相关文章:
- 使用 C++ fmtlib,有没有比使用 std::ostringstream 更干净的方法来将数据序列附加到字符串中
- 将C弦发送到非效果STD :: OSTRINGSTREAM怪异行为
- 任何无需复制即可从 std::ostringstream 获取 std::string_view 的方法
- std::ostringstream将标志放在错误的位置
- 如何移动 std::ostringstream 的底层字符串对象?
- std::ostringstream 缓冲区的最大大小是多少
- std :: ostringstream和flush成员功能
- 带有std的内存错误:OSTRINGSTREAM和-STD = C 11
- Visual Studio: "str() is not a member of std::ostringstream"
- 为什么将 std::endl 与 ostringstream 一起使用会影响输出速度
- 需要一个宏来从 std::ostringstream 和 << arg 列表创建 std::string
- 使用 Visual Studio 2013 直接从 std::ostringstream 获取 const char *
- 显式指定要写入的字符串 std::ostringstream
- c++ std::ostringstream vs std::string::append
- std::ostringstream 未返回有效的字符串
- std::ostringstream 运算符重载搜索顺序
- 查询std::ostringstream内容
- C++ std::stringstream/ostringstream and UTF characters
- C++:std::ostringstream&var的含义
- std::unique_ptr和std::ostringstream (SIGSEGV)的奇怪行为