微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C默认功能参数

我想实现这个目标:
- second parameter by default set to first argument

就像是:

int foo (int a,int b = a);

但是怎么做呢?

非常感谢!

解决方法

这是禁止的:

8.3.6认参数[dcl.fct.default]

9) Default arguments are evaluated each time the function is called.
The order of evaluation of function arguments is unspecified.
Consequently,parameters of a function shall not be used in default
argument expressions,even if they are not evaluated.
Parameters of a
function declared before a default argument expression are in scope
and can hide namespace and class member names. [ Example:

int a;

int f(int a,int b = a); / / error: parameter a

/ / used as default argument

typedef int I;

int g( float I,int b = I (2)); / / error: parameter I found

int h(int a,int b = sizeof (a )); / / error,parameter a used

/ / in default argument

—end example ]

另一种方法是重载:

int foo(int a,int b);

int foo(int a)
{
   return foo(a,a);
}

原文地址:https://www.jb51.cc/c/116713.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐