Skip to content

[内容勘误] #202

Description

@xiaoshuaijie

相关页面或文件

https://awesome-embedded-learning-studio.github.io/Tutorial_AwesomeModernCPP/vol1-fundamentals/ch03/03-overloading-default#%E5%8A%A8%E6%89%8B%E8%AF%95%E8%AF%95

问题类型

术语不准确

当前内容

规则一:默认参数必须从右向左连续出现。 编译器在处理函数调用时,只能通过"省略尾部参数"的方式来判断哪些值使用默认值。你不能跳过中间的参数——如果要给第三个参数传值,前面的所有参数都必须显式给出。所以,设计函数签名时参数的排列顺序非常重要:把最常需要自定义的参数放在最左边,把几乎不会变的参数放在最右边

// 正确:默认参数从右向左连续
void init_spi(int freq, int mode = 0, int bits = 8);

// 错误:非默认参数不能出现在默认参数后面
// void bad_init(int freq = 1000000, int mode, int bits);  // 编译错误

问题说明

“从右向左”容易让人误解为默认值的书写方向-

建议修改

默认参数只能连续放在参数列表的末尾。 从左到右看,一旦某个参数指定了默认值,它右侧的参数也必须指定默认值;调用函数时只能省略最右侧连续的一组参数,不能跳过中间参数。

补充材料

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions