dal*_*lle 5 c++ standards-compliance stringstream
tellp
我对调用空的标准行为有疑问ostringstream
。我有一个函数 foo 调用tellp
第一件事:
void foo(std::ostream& os)
{
std::ostream::pos_type pos = os.tellp();
// do some stuff.
}
int main()
{
std::ostringstream os;
foo(os);
}
Run Code Online (Sandbox Code Playgroud)
在 Visual Studio 2005 中,使用新创建的空变量调用此函数会导致ostringstream
变量pos
设置为 invalid pos_type
,而在 Visual Studio 2005 中该变量设置为pos_type(_BADOFF)
。
ofstream
没有相同的行为,其中tellp
returns pos_type(0)
,这是一个有效的pos_type
.
这个行为符合标准吗?此行为与其他编译器一致吗?
27.6.2.4:
pos_type tellp();
Run Code Online (Sandbox Code Playgroud)
返回:如果fail() != false,则返回pos_type(-1)以指示失败。否则,返回 rdbuf()->pubseekoff(0, cur , out )。
pubseekoff在失败时返回 -1。但我不确定为什么在ostringstream的情况下会发生这种情况,也许太累了而找不到有关undefined或implementation dependent的单词。根据我的常识,我会说对于ostringstream应该给出 0,对于默认构造的 ostream -1,对于新打开文件的 ostream 应该给出 0。