Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Valid expressions for default function arguments

Tags:

c++

c++-faq

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

like image 968
Emile Cormier Avatar asked Feb 15 '12 01:02

Emile Cormier


1 Answers

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

like image 193
lapk Avatar answered Oct 13 '22 01:10

lapk