I am checking the function setg()
in the class std::basic_streambuf
. It defined in the file streambuf.
The gcc version is 5.4.0
The compile option I am using is -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;
}
The member variables are declared as:
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.
I understand that since the member variables are non-const char_type*
, the function setg()
can only take non-const arguments.
But the purpose of those pointers are retrieving things, those pointers char_type *
should be declared as const char_type*
in the first place, right?
Because the member variable pointers are non-const pointers, I would assume that the values those pointers pointed to can be changed in some specific circumstances.
My question is in what situations the basic_streambuf
object will change values in the get area? If the get area content will not be changed, Is there a good reason that const pointers cosnt char_type*
should not be used?
Edit 1
In that case, the member variables can be declared as:
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.
So the setg()
can be declared as:
void
setg(char_type const* __gbeg, char_type const* __gnext, char_type const* __gend)
A bit background story about the question. My colleague modified a function foo()
which invokes setg()
so the input parameter is non-const. but the input parameters of foo()
should not be modified. Since the parameter setg()
is non-const. The function parameters of foo()
can NOT be const. The modification past his unit tests cases but failed mine due to the modification changed the input values. If the setg()
takes const in the first place, the world is better.
End of Edit 1
PS While I read the function again the variable names impressed me, the second parameter of the function setg()
named as _gnext
and it is assigned to the pointer called _M_in_cur
, I almost modified my code to increment the pointer to 1 before it passes into the function. Well done whoever named the parameter.
basic_streambuf reference
Thank you in advance for any explanation you can provide
r0nG
The get area is a buffer for reading characters from associated input sequence. If a read method is called but there are not enough characters in the get area, the basic_streambuf
object will read the associated input sequence to update the contents in the get area. In this case, the contents in the get area are modified.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With