4

Why was C++ designed like this?...

(This question is different, but intimately related to

Not possible: this pointer as a default argument. Why? )

Community
  • 1
  • 1
alexandreC
  • 481
  • 5
  • 13
  • 1
    Lots of things in C and C++ are standardized as is because they're simpler to implement using a non-dynamic approach. E. g. there could be a clause in the Standard(s) requiring that there be a mechanism for hooking/altering functions' behavior at runtime, but this would be innecessarily complicated to implement in a naive way. –  Oct 15 '12 at 20:50

2 Answers2

11

Actually, that's not completely accurate. The restrictions are:

8.3.6 Default arguments [dcl.fct.default]

7) Local variables shall not be used in a default argument. [ Example:

void f() {
int i;
extern void g(int x = i); //error
// ...
}

—end example ]

8) The keyword this shall not be used in a default argument of a member function. [ Example:

class A {
void f(A* p = this) { } // error
};

So, this and local variables can't be used as defaults.

For example, the following is valid:

int a = 1;
int f(int);
int g(int x = f(a)); // default argument: f(::a)
void h() {
  a = 2;
  {
    int a = 3;
    g(); // g(f(::a))
  }
}

g will be called with the value f(2), which isn't a compile-time constant. This is an example straight from the standard.

The reasons it's like this is the usual: there either wasn't a proposal for it, or it was rejected, deemed not necessary or too difficult to implement.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • 1
    Can you get around the restriction of using `this` by defining a member function that simply returns `this` and using it for the default? – Mark Ransom Oct 15 '12 at 21:00
  • 1
    @MarkRansom apparently not - http://ideone.com/BSjI3 - which makes sense, because you'd need `this` to call the member function, since `getA()` would be equivalent to `this->getA()`, and you can't use `this` in a default. – Luchian Grigore Oct 15 '12 at 21:02
  • @MarkRansom never thought about it before though :) – Luchian Grigore Oct 15 '12 at 21:03
  • All these have the same theme in common, and that is that the *caller* needs to be able to effectively copy-and-paste the defaults as the arguments, which is why you can't use `this`, for example. – GManNickG Oct 16 '12 at 01:38
0

Default arguments are values to a parameter due to be used in the function's body. This variable's(i.e. parameter) value may be overridden depending upon how the function is called. But while its not - the default argument is the value to this variable - and a value must be defined. It this value has to be dynamic - not to be bound during compile time - then perhaps a separate function should be better used to compute that value and does not seem fit in the default arena. Also for such a scenario C++ already has the right mechanism - Polymorphism by function overloading.

Consider this: You need to call a function Fn either with a param v or else it should default to Fn(x) here both v and x are variables.

Instead of going through having to have default parameters this could be easily implemented using function overloading.

BOOL x;

VOID Fn(BOOL v)
{
     ...
}

VOID Fn()
{
    Fn(::x);
}

VOID Main()
{
    ::x = ...; // Variable x's value is altered

    Fn(FALSE);
    Fn();      // Param value defaults to value of x
}

This enforces the programmer to write a better code which is likely to be worth its while in the longer run.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Ujjwal Singh
  • 4,908
  • 4
  • 37
  • 54
  • What's wrong with having a default in the case you presented? How does it force the programmer to write better code? Why is it better code? Usually, when you make such statements, you back them up somehow. – Luchian Grigore Oct 15 '12 at 21:30
  • @LuchianGrigore The advantage might not stand out right away - but IMO: to assign a value returned from a function is a distinct operation not suited to become a part of a function's declaration or a call. - Code readability and structure would be greatly hindered by inlining such a thing in such a place, i think. – Ujjwal Singh Oct 15 '12 at 21:39
  • I don't see how it's any different than calling an overload which passes the parameter or return value of the function to the other overload. It's the same interface. How is it better? – Luchian Grigore Oct 15 '12 at 21:41
  • In my example- `Fn()` and `Fn(BOOL)` do the same thing other than Fn(does an extra step before doing the rest common stuff. This is nicely **layered** - both *operationally* and *syntactically*. In case of the example mentioned in your answer - `g()` & `g(int x = ...)` is not the winner against the former, IMO. – Ujjwal Singh Oct 15 '12 at 21:53
  • I don't understand what `nicely layered operationally and syntactically means`. Plus, it's not. With a default, you, the programmer, see what happens when you call the function with no parameter. If you overload and don't have access to the implementation, you have no idea what argument is passed, – Luchian Grigore Oct 15 '12 at 21:56
  • @LuchianGrigore Sorry.. I had written my example wrong - changed the defaulting element to a variable. Now does it make more sense? please see the example again. – Ujjwal Singh Oct 15 '12 at 22:04
  • This still doesn't explain the last statement. The programmer will still write `Fn()` - same as he would if the function had a default - except now it's not clear what the default is. I fail to see any advantage. – Luchian Grigore Oct 15 '12 at 22:06
  • How about - the original Fn(BOOL) is untouched and additional functionality has been added, Fn(), without having to *touch* the previous code at all. – Ujjwal Singh Oct 15 '12 at 22:08
  • But then this just turns to classic overloading, and has nothing to do with default parameters (ergo, outside the scope of the question). – Luchian Grigore Oct 15 '12 at 22:09
  • the effect achieved is same - default parameters. and it has been achieved with the good old feature. My view is that the reason i present here 'could' make the case for **not** having entirely dynamic default parameters - as asked in the question. – Ujjwal Singh Oct 15 '12 at 22:19