• 14
• 12
• 9
• 10
• 9

# Complex Default Parameters?

This topic is 4915 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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();
}


##### Share on other sites
I don't have the definitive answer, but I believe the requirements are similar to code in inline functions.

##### Share on other sites
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) {}

##### Share on other sites
Quote:
 Original post by pex22but 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.

##### Share on other sites
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.

##### Share on other sites
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]

##### Share on other sites
Quote:
 Original post by TelastynHrm, 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;}

##### Share on other sites
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."

##### Share on other sites
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);