C ++:在现实世界中添加和重新定义默认参数 [英] C++: Adding and redefinition of default arguments in real world

查看:132
本文介绍了C ++:在现实世界中添加和重新定义默认参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以在C ++中添加或重新定义函数的默认参数.让我们看一下示例:

There is a possibility to add or redefine default arguments of a function in C++. Let's look at the example:

void foo(int a, int b, int c = -1) {
    std::cout << "foo(" << a << ", " << b << ", " << c << ")\n";
}

int main() {
    foo(1, 2);   // output: foo(1, 2, -1)

    // void foo(int a, int b = 0, int c);
    // error: does not use default from surrounding scope

    void foo(int a, int b, int c = 30);
    foo(1, 2);   // output: foo(1, 2, 30) 

    // void foo(int a, int b, int c = 35);
    // error: we cannot redefine the argument in the same scope

    // has a default argument for c from a previous declaration
    void foo(int a, int b = 20, int c);
    foo(1);      // output: foo(1, 20, 30)

    void foo(int a = 10, int b, int c);
    foo();       // output: foo(10, 20, 30)

    {
        // in inner scopes we can completely redefine them
        void foo(int a, int b = 4, int c = 8);
        foo(2);  // output: foo(2, 4, 8)
    }

    return 0;
}

使用的在线版本: http://ideone.com/vdfs3t

这些可能性由8.3.6中的标准规定.更具体的细节在8.3.6/4

These possibilities are regulated by the standard in 8.3.6. More specific details are in 8.3.6/4

对于非模板函数,可以在以后添加默认参数 同一范围内的函数声明.中的声明 不同的作用域具有完全不同的默认参数集. 也就是说,内部作用域中的声明不获取默认参数 来自外部作用域中的声明,反之亦然.在给定的功能 声明,参数后面的每个参数都带有默认值 参数应具有此参数或先前参数中提供的默认参数 声明或应为功能参数包.默认参数 不应由以后的声明重新定义(甚至不能相同) 价值) ...

For non-template functions, default arguments can be added in later declarations of a function in the same scope. Declarations in different scopes have completely distinct sets of default arguments. That is, declarations in inner scopes do not acquire default arguments from declarations in outer scopes, and vice versa. In a given function declaration, each parameter subsequent to a parameter with a default argument shall have a default argument supplied in this or a previous declaration or shall be a function parameter pack. A default argument shall not be redefined by a later declaration (not even to the same value) ...

说实话,我从不使用c ++编码时使用此功能.我曾多次使用类似的代码片段使同事感到惊讶,但肯定不会在生产代码中使用.因此,问题是:您是否知道使用这些功能带来好处的真​​实代码示例?

To tell the truth I never use this feature when coding in c++. I used similar code snippets several times to surprise my colleagues, but certainly not in the production code. Thus, the question is: Do you know real world examples of the code that use these features with benefits?

推荐答案

如果您无法更改某些现有代码或库,并且确实不能打扰键入正确的参数,则可以为某些范围更改默认参数可能是一个解决方案.

If you cannot change some existing code or library and you really cannot be bothered to type the correct argument then changing the default argument for some scope could be a solution.

当使用某些旧版工具生成的C ++代码时,这种破解似乎很有用.例如,如果生成的代码始终使用默认参数对数百个外部库进行了数百次调用,但现在默认参数不再正确.

It seems like the kind of hack that could be useful when working with C++ code generated by some legacy tool. For example, if the generated code has always had hundreds calls into some external library using a default argument but now the default argument is no longer correct.

这篇关于C ++:在现实世界中添加和重新定义默认参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆