默认函数参数的有效表达式

编程入门 行业动态 更新时间:2024-10-23 17:25:09
本文介绍了默认函数参数的有效表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

解决方案

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

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

更多推荐

默认函数参数的有效表达式

本文发布于:2023-11-29 23:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647933.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表达式   函数   参数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!