`basic_streambuf :: setbuf`的效果 [英] The effect of `basic_streambuf::setbuf`
问题描述
我遇到的问题如下: Martin York 声明在这, this ,and 这个答案,可以让 stringstream
使用 basic_stringbuf :: pubsetbuf
从一些内存中读取:
My problem is as follows: Martin York claims in this, this, and this answers that one can make a stringstream
read from some piece of memory by using basic_stringbuf::pubsetbuf
like this:
char buffer[] = "123";
istringstream in;
in.rdbuf()->pubsetbuf(buffer, sizeof(buffer)); // calls basic_stringbuf::setbuf
int num;
in >> num; // reads 123
不幸的是我挖掘了整个标准, 。我看到的只是实现定义。事实上对微软的实现(也许在其他人也)这个调用没有效果。
Unfortunately I dug through the whole standard and couldn't see where it's guaranteed to work. What I see is that's just implementation-defined. In fact on Microsoft's implementation (maybe on others too) this call has no effect.
这里是相关的引用,我发现在最后一个C ++ 0x草案。对于 basic_streambuf :: setbuf
[streambuf.virt.buffer]:
Here are related quotes I found in the last C++0x draft. For the basic_streambuf::setbuf
[streambuf.virt.buffer]:
1 效果:影响流缓冲的方式,对于从本条款(27.8.1.4,27.9.1.5)中的basic_streambuf派生的每个类别单独定义。
1 Effects: Influences stream buffering in a way that is defined separately for each class derived from basic_streambuf in this Clause (27.8.1.4, 27.9.1.5).
2 默认行为: 什么也不做。返回this。
2 Default behavior: Does nothing. Returns this.
然而在派生类中,它似乎保留了执行定义的行为。 basic_stringbuf :: setbuf
它说[stringbuf.virtuals]:
However in the derived classes it seems to leave the behavior implementation-defined. For basic_stringbuf::setbuf
it says [stringbuf.virtuals]:
em>
1 Effects: implementation-defined, except that setbuf(0,0) has no effect.
basic_filebuf :: setbuf
它表示[filebuf.virtuals]:
For basic_filebuf::setbuf
it says [filebuf.virtuals]:
12 效果:如果setbuf(0,0)[...],则流变得无缓冲。否则结果为实施定义。 Unbuffered[...]
12 Effects: If setbuf(0,0) [...], the stream becomes unbuffered. Otherwise the results are implementation-defined. "Unbuffered" [...]
就是这样。所以我认为,一个有效的实现可以完全忽略这些调用(对于非null参数)。
And that's it. So as I see it, a valid implementation can ignore these calls completely (for non-null parameters).
我错了吗?什么是标准的正确解释? Do C ++ 98/03 / 0x有相同的保证吗?你有更多的统计数据上面的代码工作的实现和它不是?如何 basic_streambuf :: setbuf
打算使用?
Am I wrong? What is the correct interpretation of the standard? Do C++98/03/0x have the same guarantees? Do you have more statistics on which implementations the above code works and on which it does not? How basic_streambuf::setbuf
is intended to be used?
推荐答案
相信它是实现定义的,并且您提供了相关的引号。
I believe it is implementation defined and that you provided the relevant quotes.
为了记录,这是标准C ++ IOStreams和语言环境,不是我不得不承认的最近一本书, >几乎语义的自由函数 setbuf()
:
For the record, this is what Standard C++ IOStreams and locales, not exactly a recent book I have to admit, has to say on the subject in a section titled "The almost semantic free function - setbuf()
" :
虚拟成员函数
setbuf()
是一个相当奇怪的流缓冲区
成员。 它的语义基本上是
undefined 。对于字符串流缓冲区,
setbuf()
的语义是
实现定义的,除了
setbuf (0,0)
定义:如果
setbuf(0,0)
在流
之前调用I / O发生在
流上,流变为未缓冲,
意味着字符直接
传输到文件
系统。 否则,结果是
实现定义的。
The virtual member function
setbuf()
is a rather peculiar stream buffer member. Its semantics are basically undefined. For string stream buffers, the semantics ofsetbuf()
are implementation-defined, except thatsetbuf(0, 0)
are defined: ifsetbuf(0, 0)
is called on a stream before and I/O has occured on that stream, the stream becomes unbuffered, meaning that characters are directly transported to and from the file system. Otherwise, the results are implementation-defined.
但是,
setbuf()
basic_filebuf
和
basic_stringbuf
b对其他流缓冲区
中的
setbuf()
语义的要求。一般来说,一般语义
可以被定义为设备,并且在用户定义的流缓冲器
类型的
中,实现特定。
However, the specifications of
setbuf()
for basic_filebuf
and
basic_stringbuf
hardly impose any
requirements on the semantics of
setbuf()
in other stream buffer
types. At best, the general semantics
can be defined as device and, in the
case of user-defined stream buffer
types, implementation-specific.
缺少任何需求可以释放你
重新定义 setbuf()
,只是
的任何目的,任何方式适合
到
setbuf()
的预定义接口中。
The lack of any requirements frees you
to redefine setbuf()
for just about
any purpose and in any way that fits
into the predefined interface of
setbuf()
.
这篇关于`basic_streambuf :: setbuf`的效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!