为什么函数 std::basic_streambuf::setg() 采用非常量参数

r0n*_*0ng 5 c++ pointers constants

我正在检查setg()班级中的功能std::basic_streambuf。它在文件 streambuf 中定义。

gcc 版本是 5.4.0

我使用的编译选项是 -std=gnu++14

void
setg(char_type* __gbeg, char_type* __gnext, char_type* __gend)
{
    _M_in_beg = __gbeg;
    _M_in_cur = __gnext;
    _M_in_end = __gend;
}
Run Code Online (Sandbox Code Playgroud)

成员变量声明为:

char_type*      _M_in_beg;     ///< Start of get area.
char_type*      _M_in_cur;     ///< Current read area.
char_type*      _M_in_end;     ///< End of get area.
char_type*      _M_out_beg;    ///< Start of put area.
char_type*      _M_out_cur;    ///< Current put area.
char_type*      _M_out_end;    ///< End of put area.
Run Code Online (Sandbox Code Playgroud)

我知道由于成员变量是非常量的char_type*,所以函数setg()只能采用非常量参数。但是这些指针的目的是检索东西,这些指针char_type *应该首先声明为const char_type*,对吗?

因为成员变量指针是非常量指针,我假设这些指针指向的值在某些特定情况下可以更改。

我的问题是在什么情况下basic_streambuf对象会改变获取区域中的值?如果不会更改获取区域内容,是否有充分的理由cosnt char_type*不应使用const 指针?

编辑 1
在这种情况下,成员变量可以声明为:

char_type const *_M_in_beg;     ///< Start of get area.
char_type const *_M_in_cur;     ///< Current read area.
char_type const *_M_in_end;     ///< End of get area.
char_type       *_M_out_beg;    ///< Start of put area.
char_type       *_M_out_cur;    ///< Current put area.
char_type       *_M_out_end;    ///< End of put area.
Run Code Online (Sandbox Code Playgroud)

所以setg()可以声明为:

void
setg(char_type const* __gbeg, char_type const* __gnext, char_type const* __gend)
Run Code Online (Sandbox Code Playgroud)

关于这个问题的一些背景故事。我的同事修改了一个foo()调用函数,setg()因此输入参数是非常量的。但foo()不应修改的输入参数。由于参数setg()是非常量的。的函数参数foo()不能为常量。修改通过了他的单元测试案例,但由于修改改变了输入值而导致我的失败。如果setg()首先采用 const,世界会更好。
结束编辑 1

PS 当我再次阅读函数时,变量名给我留下了深刻的印象,函数的第二个参数setg()命名为 as_gnext并且它被分配给名为 的指针_M_in_cur,我几乎修改了我的代码以在指针传递到函数之前将指针增加到 1。命名参数的人做得好。

basic_streambuf 参考

预先感谢您提供的任何解释

r0nG

xsk*_*xzr 2

获取区域是用于从相关输入序列读取字符的缓冲区。如果调用 read 方法但获取区域中没有足够的字符,则对象basic_streambuf将读取关联的输入序列以更新获取区域中的内容。此时,获取区域的内容被修改。