Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Adding and redefinition of default arguments in real world

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;
}

Online version to play with: http://ideone.com/vdfs3t

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) ...

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?

like image 737
DAle Avatar asked Jun 29 '17 07:06

DAle


People also ask

What are default arguments demonstrate its use with proper example?

Default arguments are overwritten when the calling function provides values for them. For example, calling the function sum(10, 15, 25, 30) overwrites the values of z and w to 25 and 30 respectively.

In which situation default arguments are useful?

D. Default arguments are useful in situations where some arguments always have the same value.

What is the principle reason for using default arguments in the function?

The default arguments are used when you provide no arguments or only few arguments while calling a function. The default arguments are used during compilation of program.

Why do we need to use default arguments in function explain with the help of C++ program?

In C++ programming, we can provide default values for function parameters. If a function with default arguments is called without passing arguments, then the default parameters are used. However, if arguments are passed while calling the function, the default arguments are ignored.


1 Answers

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.

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.

like image 148
snow_abstraction Avatar answered Oct 15 '22 11:10

snow_abstraction