草庐IT

c++ - 无法使用 __gnu_cxx::stdio_filebuf 进行流式传输

这会创建文件,但不会写入任何内容。std::ofstreamoutstream;FILE*outfile;outfile=fopen("/usr7/cs/test_file.txt","w");__gnu_cxx::stdio_filebuffilebuf(outfile,std::ios::out);outstream.std::ios::rdbuf(&filebuf);outstream我知道还有其他简单的解决方案可以实现输出,但我需要在编辑时使用这个非标准的filebuf来锁定文件,这样其他进程就无法打开文件。我不知道为什么这不起作用。 最佳答案

通用模板 ostream << 运算符的 C++ 不明确重载

这个问题紧接我之前的问题:Genericoperator我想在哪里实现一个通用的适用于拥有to_str()的任何类的运算符方法。我已经成功地检查了一个类是否实现了to_str()方法与用途std::cout感谢这个answer.但是,我在编写模板时遇到困难ostream运营商制作std::cout有效。以下测试代码:#include#include#includetemplateusingvoid_t=void;templatestructhas_to_string:std::false_type{};templatestructhas_to_string().to_str())>>:

c++ - 如何在没有此运算符的情况下为类型实现默认运算符<<(ostream&, T)?

自std::to_string添加到c++11,我开始实现to_string而不是更传统的operator.我需要将两者链接在一起,以便合并依赖operator的库.我希望能够表达如果T有operator,用它;否则,使用std::to_string.我正在制作一个更有限的版本的原型(prototype),该版本支持operator对于所有枚举类。enumclassMyEnum{A,B,C};//plainversionworks//std::ostream&operator::value>::type>std::ostream&operator编译器说error:nomatchfor

c++ - std::experimental::ostream_joiner 和 std::pair

在c++17/g++7中,终于有了怀念已久的ostream_joiner。它可以正确输出到ostream,使用中缀定界符分隔集合元素。#include#include#include#include#include#includeusingstring=std::string;#if1structpair{stringfirst;stringsecond;};#elseusingpair=std::pair;#endifstd::ostream&operatorpairs={{"foo","bar"},{"baz","42"}};std::copy(std::begin(pairs),

c++ - 为什么简单地使用 ostringstream 会生成这么多汇编代码?

考虑以下使用ostringstream格式化字符串和整数并丢弃输出的简单示例:#includevoidostringstream_test(){std::ostringstreamss;ss使用clang++-S-O3-DNDEBUG-std=c++14test.cc编译它会生成大量汇编代码(x86-64指令为半千字节,而不到百字节的类似sprintf代码)-请参阅下面的输出。为什么它会生成这么多代码,是ostringstreamAPI固有的还是这个特定的编译器/库做错了什么?.globl__Z18ostringstream_testv.p2align4,0x90__Z18ostrin

c++ - 为什么 ostream::operator<< 是 char 参数的全局函数?

根据http://www.cplusplus.com/reference/iostream/ostream/operator%3C%3C/operator 最佳答案 operator对于streambuf*(或int这听起来更简单)和char既可以作为成员(member)运营商实现,也可以作为非成员(member)(免费)运营商实现。我的猜测是,这是由于在定义C++时出现了追溯兼容性问题:可能较旧的代码依赖于成员operator,因此他们决定不将其作为免费运营商移动。C++标准库(以及STL)有许多像这样的不均匀性。

c++ - istream 和 ostream 跨平台

假设我想在我的大端机器上写这个an_ostream_implmy_output_on_BE;my_output_on_BE这是在我的小端机器上an_istream_implmy_input_on_LE;__int32value;my_input_on_LE>>value;assert(value==0x1234);是否有允许这样做的istream/ostream实现?例如。总是以BigEndian(或任何格式)流式传输数字? 最佳答案 如果您需要在机器之间共享比单个整数更复杂的数据,我衷心推荐GoogleProtocolBuffer

c++ - 将字符串写入 ostream

当我尝试编译下面的代码时(在使用llvm-g++-4.2(GCC)4.2.1的Qt4.8中),出现以下错误:../GLWidget.cpp:24:instantiatedfromhere../GLWidget.cpp:24:error:explicitinstantiationof'std::basic_ostream&std::operator&,conststd::basic_string&)[with_CharT=char,_Traits=std::char_traits,_Alloc=std::allocator]'butnodefinitionavailable此错误是什么意

c++ - 如何将 ostream 重定向到 Boost Log 库

我有一个进度条函数,它将std::ostream作为参数。出于描述的目的,我在这里对其进行了简化。voidsomeprogressbar(std::ostream&stream){stream我无法修改此功能,因为它是第三方功能。我用std::ostringstreammyoss;someprogressbar(myoss)调用这个函数或someprogressbar(std::cout).该函数会在我的程序进行时实时打印一些信息。如何将输出重定向到BoostLog库?我能做到BOOST_LOG_TRIVIAL(debug),但做不到someprogressbar(BOOST_LOG_

c++ - 使用 rdbuf 复制流在输入为空时失败

使用rdbuf将流复制到另一个流是一种众所周知的方法:#include#includeintmain(){std::ifstreamin{"/tmp/foo.txt"};std::cerr但是,当/tmp/foo.txt为空时,这会破坏(=设置错误位)我的cerr。因此,不会显示Done\n。这是为什么呢?使用G++/libstdc++/GNULinux和Clang++/libc++/OSX观察。 最佳答案 这似乎是定义的行为-参见例如http://en.cppreference.com/w/cpp/io/basic_ostream