I have some questions about the default values in a function parameter list
1) Is the default value a part of the signature? What about parameter type of the default parameters?
2) Where are the default value stored? In the stack or or global heap or in the constant data segment?
Thanks!
Default values, in the context of databases, are preset values defined for a column type. Default values are used when many records hold similar data.
The Default value for static variables in C language is zero. The static variable memory can be used until the program completes. The default value of static values is zero.
A default argument is a value provided in a function declaration that is automatically assigned by the compiler if the calling function doesn't provide a value for the argument. In case any value is passed, the default value is overridden.
Default Parameter Data TypesAny primitive value or object can be used as a default parameter value.
No, default argument is not a part of signature and is not a part of the function type.
Parameter type is a part of signature. But default argument type has no effect of parameter type, i.e default argument type has no effect on signature.
Default arguments are not "stored" anywhere specifically. Default arguments are "syntactic sugar" that exists (as default arguments) only during the program's compilation. If during the compilation compiler notices that some argument is missing, it will use the default argument, as specified by you. The evaluation of the default argument is done in the context of the caller. If you specify a temporary object as a default argument, a separate temporary will be created every time you call the function using the default argument and destroyed immediately after the calling expression ends.
void foo(T t = T());
// A new temporary will be used as an argument every time you call it as `foo()`
foo();
// Equivalent to `foo(T())`. A temporary is created here, passed as an argument and
// destroyed afterwards, just like an explicit temporary would be
If you specify an existing object with static storage duration as a default argument, then it will be stored wherever you define it.
T global;
void foo(T& t = global);
// In this case you are using a global object as a default argument
// It is you who "store" it wherever you want to store it
foo();
// Equivalent to `foo(global)`
If you declare default arguments but never actually use them, i.e. if you specify the arguments explicitly every time, then the compiled program will have no trace of these arguments whatsoever (which is why I called them compile-time "syntactic sugar").
P.S. To include what Johannes says in the comment below: even though the default argument (when used) is evaluated in the context of the caller at the moment of the call, it is not done by "textual substitution" as in might appear from my examples above. Most notably, the name lookup for the names used in default arguments is done at the point when the default argument is specified in the function declaration, not at the point of the evaluation in the caller.
1) Is the default value a part of the signature?
No.
What about parameter type of the default parameters?
The type is always in the signature (if you don't extern "..."
) regardless it has default values or not.
2) Where are the default value stored?
Nowhere. They are filled in by the compiler automatically.
1) The type/signature of a function which has default parameters is the same as the type/signature of the function if the parameter was there without any default value:
// foo1 and foo2 are both functions taking an int, and returning an int.
int foo1(int);
int foo2(int a = 0);
typedef int(*int_fn_ptr)(int);
int_fn_ptr f1 = foo1; // OK, assigning a function pointer
int_fn_ptr f2 = foo2; // OK
typedef int(*void_fn_ptr)(void);
void_fn_ptr f3 = foo2; // doesn't compile
2) Default values are created by the caller when the function is called. In effect, the compiler just replaces:
foo2();
with:
foo2(0);
as soon as it sees it. This is why default values have to be specified at the place the function is declared, where callers can see them.
In some other languages, the default value "belongs" to the function itself. It might make sense to ask where that default value is "stored", but this is not the case in C++.
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