给定相同的索引,是否保证 iword() 和 pword() 引用不同的基础值?

Given the same index, are iword() and pword() guaranteed to refer to separate underlying values?

在标准 C++ ios 库中,是否保证 &stream.iword(ind) != &stream.pword(ind) 在同一个 stream 上调用时具有相同的 ind 值?

此外,是否保证我可以在给定相同索引的情况下单独使用 void*long 值(即它们未作为联合实现等)?

我想这里的 X-Y 问题是我如何知道给定一个随机流,我的自定义值("allocated" 使用 xalloc())是否已经初始化?我问是因为我看到人们使用 pword() 检查魔术常量,然后 iword() 在前面的条件失败时初始化该值。

我主要关心 C++11 及更高版本,但欢迎提供任何相关历史信息。

C++ 标准规定 iword()pword() 应该表现得好像它们正在操纵两个独立的存储块 - 而不是,比如说,一个块,其内容被解释为 long 有时 void* 其他时候。

[ios.base]

namespace std {
  class ios_base {
  // ...
  private:
    long* iarray; // exposition only
    void** parray; // exposition only
  };
}

[ios.base.storage]/3

  long& iword(int idx);

Effects: If iarray is a null pointer, allocates an array of long of unspecified size and stores a pointer to its first element in iarray. The function then extends the array pointed at by iarray as necessary to include the element iarray[idx]. Each newly allocated element of the array is initialized to zero. The reference returned is invalid after any other operations on the object. However, the value of the storage referred to is retained, so that until the next call to copyfmt, calling iword with the same index yields another reference to the same value...

[ios.base.storage]/5 重复完全相同的措辞,用 pword 代替 iword,用 parray 代替 iarrayvoid* 对于 long.

重要的部分是 "the value of the storage ... is retained" - 使用给定索引的后续 iword 调用应该看到写入存储的值,而之前的 iword 使用相同索引调用提供了一个参考,不管中间方法调用(copyfmt 除外),即使中间方法调用是 pword(反之亦然)。