tags:

views:

184

answers:

4

why default arguments in C++ are trailing ones ?

+12  A: 

if you had void func(int a = 0, int b);, how would you specify to use the default parameter in calling this function?

tenfour
I know you can't in C++, but other languages handle this with `func(,some_val_for_b)`, or named parameters.
Dominic Rodger
@Dominic Rodger: to me that looks really ugly.
dierre
@Dominic Rodger, well, C++ doesn't. If you want you can sue Stroupstroup :P
klez
Stroupstroup: nice :)
Skilldrick
`Looks ugly` has never been a design criteria of C++.
Luther Blissett
What's the problem? If you pass one argument, it clearly gets bound to `b` (because where else would it go?) and if you pass two arguments, the first one gets bound to `a` and the second one gets bound to `b`, because, again, nothing else makes sense. There's no ambiguity there. A list is symmetric, after all, it looks and works the same from either side. So, if you believe that putting default arguments at the end of the argument list is okay, and since lists are symmetric, you logically *have* to accept the fact that putting them at the beginning works just as well.
Jörg W Mittag
In fact, you can even put them in the middle. It works just fine in Ruby, for example, and Ruby doesn't even have the help of a static type signature to figure out which argument goes where.
Jörg W Mittag
A: 

Because in a function call you have to call the non-default arguments in any case. If you put your default argument at the beginning of the argument list, how are you supposed to say you are setting the default argument or the other ones?

klez
What is it that makes the beginning of a list so fundamentelly different from the end of the list? Lists are symmetrical!
Jörg W Mittag
Yes, but imagine this: void func(int a, int b=0, int c, int d=0); What are you doing when you call func (1, 2, 3);
klez
And which end of the list, exactly, is `b` on?
Jörg W Mittag
The 'end onf the list' issue was just an example
klez
@JW Mittag "lists are symmetrical" - you mean, like a singly-linked list, where navigation is one-way ?
jdu.sg
@Jörg: You mentioned that Ruby doesn't have a problem with default arguments anywhere. So how does it interpret klez's example (or even `void func(int a = 0, int b, int c, int d = 0); func(1, 2, 3);`)?
UncleBens
@jdu.sg: Exactly. It doesn't matter whether you draw the head on the left side and all the arrows pointing to the right or the head on the right side and all the arrows pointing to the left. Likewise, it doesn't matter whether you put your optional arguments on the left side of the argument list or the right side, because it's symmetrical: depending on where you look from, it's still the same side. Since default arguments on the right side obviously work (after all, they are in the standard, and are implemented), this means that default arguments on the left side must also obviously work.
Jörg W Mittag
@UncleBens: I never said that Ruby doesn't have a problem with default arguments anywhere. I said it doesn't have problems with default arguments in the middle. Your example has them on either end, and @klez's example has them all over the place. Maybe I should have better said that Ruby allows mandatory parameters at both ends. I particular, it's `mand* opt* rest? mand*`, i.e. 0 or more mandatory, followed by 0 or more optional, followed by an optional rest parameter, followed by 0 or more mandatory parameters. Arguments get bound to the mandatory parameters from both ends of the argument ...
Jörg W Mittag
... list, then, optional parameters get bound left-to-right from the remaining argument list, and any leftover arguments get packaged in an array and bound to the rest parameter. If at any point during this algorithm you end with mandatory parameters left but no arguments to bind, or with arguments left, but no parameters to bind them to, you get an `ArgumentError` exception.
Jörg W Mittag
+1  A: 

Just to supplement @tenfour's answer. C++ FAQ Lite has a topic describing named parameters and I think the topic addresses your issue to some extent.

skwllsp
+3  A: 

Because that is how the language has been designed.

A more interesting question would be: what are the alternatives ?

Suppose you have void f(A a = MyA, B b);

  • Placeholder / blank argument: f(_, abee) or f(, abee)
  • Named arguments (like in Python): f(b = abee)

But those are niceties and certainly not necessary, because unlike Python C++ supports function overloading:

  • void f(A a, B b);
  • void f(B b) { f(MyA, b); }

and thus the default arguments are unnecessary... especially considering that there are issues when used with OO-code because default arguments are statically resolved (compile-time)

struct Base
{
  virtual void func(int g = 3);
};

struct Derived
{
  virtual void func(int g = 4);
};

int main(int argc, char* argv[])
{
  Derived d;
  d.func(); // Derived::func invoked with g == 4

  Base& b = d;
  b.func(); // Derived::func invoked with g == 3 (AH !!)
}

EDIT: on named parameters ->

The feature can be emulated using function objects.

class Func
{
public:
  Func(B b): mA(MyA), mB(b) {}

  A& a(A a) { mA = a; }
  B& b(B b) { mB = b; }

  void operator()() { func(mA, mB); }
private:
  A mA;
  B mB;
};

int main(int argc, char* argv[])
{
  A a; B b;
  Func(b)();
  Func(b).a(a)();
}

In case you don't want to copy the arguments, you have the possibility to use references/pointers though it can get complicated.

It's a handy idiom when you have a whole lot of defaults with no real order of priority.

Matthieu M.
Function overloading doesn't help if `A` and `B` are both the same type...
Oli Charlesworth
@Oli: why ? given the first `func` prototype it's clear that if there is only one parameter then the default value should be used for the first parameter, whatever their type. Things get hairy when you have two default though ;) In general, named parameters are the only thing that may solved the issue, and what's great is that they are a compile-time solution.
Matthieu M.
@Matthieu: Yes, you are right. I thought you were using overloading to disambiguate `func(A a, B b)`, `func(B b)` *and* `func(A a)`.
Oli Charlesworth