问题描述
我遇到的问题如下:声明在,,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]:
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]:
basic_filebuf :: setbuf 它表示[filebuf.virtuals]:
For basic_filebuf::setbuf it says [filebuf.virtuals]:
就是这样。所以我认为,一个有效的实现可以完全忽略这些调用(对于非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.
为了记录,这是,不是我不得不承认的最近一本书, >几乎语义的自由函数 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() 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`的效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!