过载与c标准中的默认参数

过载与c标准中的默认参数,第1张

概述我正在阅读另一个问题,它让我想起了.标准通常指定在其描述中具有默认参数的函数.标准是否允许将这些写入重载代替? 例如,标准说std :: basic_string :: copy有以下声明: size_type copy(Ch* p, size_type n, size_type pos = 0) const; 标准库的一致性实现可以实现这两个这样的功能吗? size_type copy(Ch* 我正在阅读另一个问题,它让我想起了.标准通常指定在其描述中具有默认参数的函数.标准是否允许将这些写入重载代替?

例如,标准说std :: basic_string :: copy有以下声明:

size_type copy(Ch* p,size_type n,size_type pos = 0) const;

标准库的一致性实现可以实现这两个这样的功能吗?

size_type copy(Ch* p,size_type pos) const;size_type copy(Ch* p,size_type n) const;

在这个例子中,第二个版本可以跳过if(pos> size()){throw out_of_range(); }测试是第一个必要的.一个微优化,但是你仍然看到了这个例子.

解决方法

Could a conforming implementation of the standard library implement this instead as two functions like this?

是. C标准(C 03 17.4.4.4/2-3)说:

An implementation can declare additional non-virtual member function signatures within a [Standard library] class:

— by adding arguments with default values to a member function signature; the same latitude does not extend to the implementation of virtual or global or non-member functions,however.

— by replacing a member function signature with default values by two or more member function signatures with equivalent behavior;

— by adding a member function signature for a member function name.

A call to a member function signature described in the C + + Standard library behaves the same as if the implementation declares no additional member function signatures

总结

以上是内存溢出为你收集整理的过载与c标准中的默认参数全部内容,希望文章能够帮你解决过载与c标准中的默认参数所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1250726.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存