4

I was going through the implementation of seekpos in the source of streambuf in the gnu online doc. I couldn't understand why __mode is commented in the line ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out and why it doesn't throw an error.

  virtual pos_type 
  seekpos(pos_type, ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out)
   {
      return pos_type(off_type(-1));
   }

I can understand the usage of the comment, if it was of the following format :

void foo( pos_type, int /*blah*/ ){
...
}

But, in the former case, there is also an intention to assign something to __mode, hence my surprise on not getting any error there.

Is this allowed? If yes, then why ?

bipll
  • 11,747
  • 1
  • 18
  • 32
Vishaal Shankar
  • 1,648
  • 14
  • 26
  • Not quite a duplicate, but there are some insightful comments to the very similar question here: [Can an unnamed parameter of function have a default value?](https://stackoverflow.com/questions/14675908/can-an-unnamed-parameter-of-function-have-a-default-value?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) – acraig5075 May 18 '18 at 12:26

1 Answers1

5

First of all it's not an assignment, it's a default argument. And you can have any or even all arguments be anonymous.

If an argument is not used inside the function, you can leave out the variable name, and just have the type, and (as noted) even default arguments. The argument variable name is optional.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Agreed, I wanted to mention argument. And i know, we can just have the type, as shown in the second example. I just don't understand the purpose of having `int /*blah*/ = 2` – Vishaal Shankar May 18 '18 at 12:09
  • 1
    well to some extend I can answer my own question: only with the default the method can be called with one or with two arguments, while the parameter being using inside the method is an implementation detail – 463035818_is_not_an_ai May 18 '18 at 12:11
  • @user463035818 Just because it's the right format for the parser doesn't mean it has value. Sometimes it's easier (in the standard and the impls of it) to just allow something to be ignored, than to explicitly require it to be rejected ... the point in this example would be less changes to remove the variable, but after that; clearly the answer is "there is no point". – UKMonkey May 18 '18 at 12:35
  • @UKMonkey: parameter cannot be removed as method is virtual (except is all classes doesn't use the argument). – Jarod42 May 18 '18 at 12:53
  • @Jarod42 I didn't say parameter, I said variable. It has been essentially removed because it has no name and thus cannot be accessed (but I agree that it will still take up space in memory). – UKMonkey May 18 '18 at 13:01
  • @user463035818 thinking about it harder - there is a point. What if the class of the unnamed variable, on creation, has a side effect? – UKMonkey May 18 '18 at 13:03
  • @UKMonkey: or even at destruction ;-) – Jarod42 May 18 '18 at 13:13
  • The main thing that I would want to know here is why would I want something of this sort. It'd be great if there is an example to follow the reasoning's mentioned above.( Sort of an advantage of having an unnamed default parameter ). – Vishaal Shankar May 20 '18 at 14:58
  • @VishaalShankar If you have a name for the argument but don't use it, it might cause the compiler to give a warning about unused variables. And using a default value is the same no matter if the argument name is given or not. – Some programmer dude May 20 '18 at 15:00
  • @Someprogrammerdude : I understand that. I am aware of the warnings that show up if it is an unused variable. What I am pondering about here is **Why do they have that in a standard library method and what they achieve out of it**. Since `__mode` is inaccesible, isn't it same as not having it at all ? Do correct me if I am wrong here. – Vishaal Shankar May 20 '18 at 15:18
  • I hope it is clear what I am trying to understand. If not, I will try to make myself clearer. – Vishaal Shankar May 20 '18 at 15:19
  • 1
    @VishaalShankar Assuming you mean one of the stream buffers `seekpos` function, then they are *specified* to take a default argument. And it takes a default argument so you don't have to pass that argument when you call the function. You *do* know what default arguments are used for? You can call `seekpos(some_position)` or `seekpos(some_position, some_open_modes)`. As I said before, it's kind of like doing function overloading. – Some programmer dude May 20 '18 at 15:21