8

What are all the possible types of valid expressions for a default argument in a function or member function?

4
  • 1
    You need to be a lot more specific. Commented Feb 15, 2012 at 1:45
  • 2
    My question is purposely general. I want to know what types of expressions that I can use as a default argument. SO is not just about asking for help fixing a bug. Commented Feb 15, 2012 at 1:48
  • msdn.microsoft.com/en-us/library/e1dbzf09.aspx Commented Feb 15, 2012 at 1:51
  • ¤ One special expression you can't use is void(). Unfortunately Visual C++ 10.0 accepts it. Other than that there is no restriction on argument default expressions compared to expressions in general. (Except if you're thinking of non-copyable types or such, which is not a restriction on the expression kind but on the type). If you are interested in the syntactical categories, then consult the C++ BNF grammar. Cheers & hth. Commented Feb 15, 2012 at 1:52

2 Answers 2

5

Anything that is correct within context of assignment to a variable of function parameter's type.

Edit
The default arguments during compilation are evaluated in terms of type correctness etc, but they are not calculated and no assignment takes place until run-time. You can specify a constructor of a yet to be defined class as a default argument and it's fine, as long as class is defined at the point of function use... The actual calculation/assignment takes place during function call, not at the point of function declaration/definition.

Example:

#include <iostream>

void foo( int a = std::rand())
{
  std::cout << a << std::endl;
}

int main( void )
{
 foo();

 return( 0 );
}

Program output on ideone.com:

1804289383

Sign up to request clarification or add additional context in comments.

4 Comments

This is false. You can assign things to variable that can only be determined at runtime, but you wouldn't be able to use them as default arguments. For example, you can do int a = std::rand(), but you can't do void Class::foo(int a = std::rand()).
Oops, sorry. I'm letting my false preconceptions get the better of me again.
@EmileCormier The default arguments during compilation are evaluated in terms of type correctness etc, but they are not calculated and no assignment takes place until run-time. You can specify a constructor of a yet to be defined class as a default argument and it's fine, as long as class is defined at the point of function use... The actual calculation/assignment takes place during function call, not at the point of function declaration/definition.
+1 Thanks for the clarification, and again, sorry about my crazy antics. :-)
4

This is detailed in section 8.3.6 of the C++03 standard. It basically amounts to any expression that doesn't depend on anything in local scope, so any expression which depends on local variables, parameters to a function, or on "this" are excluded.

1 Comment

This compliments AzzA's answer very well. I wish I could accept both answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.