Complex Default Parameters?

Started by
7 comments, last by joanusdmentia 19 years, 6 months ago
Using C++, programmers are able to specify default parameters for functions should the parameter not be specified:

void printint(int foo=5){
    cout << foo << "\n";
}

printint();
psuedo code would print 5 for example. What [if any] formal limitations apply to the assignment there? For example, the following code actually compiles nicely and does 'as expected':

int bar(){
    return(5);
}

void foo(int f=bar()){
    cout << f << "\n";
}

void main(){
    foo();
}
Advertisement
I don't have the definitive answer, but I believe the requirements are similar to code in inline functions.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
you cant do:
void func(int a=1,int b);/* or */void func(int a=1,int b,int c=2);/* or */void func(int a=1);func(int a=1) {} /* redefinition error,the default parameter must be in the prototype OR function definition.cant be in both */

but i think this will compile fine:
void func(int a=1,int b);func(int a,int b=2) {}
pex.
Quote:Original post by pex22
but i think this will compile fine:
void func(int a=1,int b);func(int a,int b=2) {}
Nope. "error C2548: 'func' : missing default parameter for parameter 2". The compiler needs to know the default arguments when the call is made, so they can be passed if the code doesn't supply any default args.
Hrm, I wonder if my compiler simply replaced bar in the example code with 5. I can't seem to find a nice definative answer online. Most places seem to imply that it should not work.

You have to supply default parameters for all of the params from the first with a default param through to the last param.
void func(int a,   int b,   int c=1); // OKvoid func(int a,   int b=1, int c=2); // OKvoid func(int a,   int b=1, int c);   // ERRORvoid func(int a=1, int b,   int c=2); // ERROR

As for what's valid as part of the assignment, I think that anything that is valid for defining a local variable will also be valid for defining a default parameter. Although, I'm not 100% sure on this. Best way to find out is try it out.

Well, actually the best way is to go read the standard, second best way is to try it out. [smile]
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
Quote:Original post by Telastyn
Hrm, I wonder if my compiler simply replaced bar in the example code with 5. I can't seem to find a nice definative answer online. Most places seem to imply that it should not work.

Try this:
void foo(int f=rand()){    std::cout << f << std::endl;}
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
heh, yes yes, I know the limitations to normal param=constant default params. I was suprised that using a function's return value as a default parameter worked.

Anyways, I have found a more definative answer from msdn:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/ators_32.asp

"The expression can combine functions that are visible in the current scope, constant expressions, and global variables. The expression cannot contain local variables or non-static class member variables."
Quote:Original post by Telastyn
"The expression can combine functions that are visible in the current scope, constant expressions, and global variables. The expression cannot contain local variables or non-static class member variables."

Makes sense, since this isn't in scope yet. In the same way you can't do
void func(int a, int b=a);
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V

This topic is closed to new replies.

Advertisement