对于函数或成员函数中的默认参数,有效表达式的所有可能类型是什么?
解决方案
编辑 编译期间的默认参数是在类型正确性等方面进行评估,但是它们不被计算,并且直到运行时间才发生分配。您可以将尚未定义的类的构造函数指定为默认参数,只要在函数使用点定义类就可以了...实际的计算/赋值在函数调用期间发生,而不是在点处
c $ c> #include< iostream> void foo(int a = std :: rand()) { std :: cout< a<< std :: endl; } int main(void) { foo(); return(0); }ideone上的计划输出:
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:
1804289383
更多推荐
默认函数参数的有效表达式
发布评论