默认函数参数的有效表达式 [英] Valid expressions for default function arguments

查看:125
本文介绍了默认函数参数的有效表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于函数或成员函数中的默认参数,有效表达式的所有可能类型是什么?

解决方案



编辑

编译期间的默认参数是在类型正确性等方面进行评估,但是它们不被计算,并且直到运行时间才发生分配。您可以将尚未定义的类的构造函数指定为默认参数,只要在函数使用点定义类就可以了...实际的计算/赋值在函数调用期间发生,而不是在点处



 <$> c $ c> #include< iostream> 

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

int main(void)
{
foo();

return(0);
}

ideone.com上的计划输出



1804289383


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

解决方案

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

这篇关于默认函数参数的有效表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆