Function call with default parameter

I wrote a C ++ programming exam. There was one question that my professor and I did not agree to. The question is whether the following function works:

#include <iostream> using namespace std; void f(int=4, long=10, double=3.14); int main( int argc , char ** argv ) { f( , ,8); return EXIT_SUCCESS; } void f(int i, long l, double d) { cout << i << " " << " " << l << " " << d; } 

I said that this will not work, but my professor said that this will definitely work because of the default parameter in the function declaration. I tried this with MSVC and it did not work. Is this compiler specific? How can I convince my professor that he does not work in any compiler to raise my mark in the exam?

+4
source share
4 answers

To begin with, you are right, the professor is mistaken. And the fact that he claims that this should work (when it is trivial to show that it is not) means that he probably should not teach C ++. But how to show him?

Just try compiling it with at least two different compilers. If both do not compile it, then there is a very good chance that this is invalid code. This is the fastest way.

If this fails, specify the C ++ standard. Here is the syntax of the call expression, as described in [expr.post] p1 and after nonterminal links:

 postfix-expression: ... postfix-expression ( expression-list[opt] ) expression-list: initializer-list initializer-list: ([dcl.init]p1) initializer-clause ...[opt] initializer-list , initializer-clause ...[opt] initializer-clause: assignment-expression braced-init-list braced-init-list: { initializer-list ,[opt] } { } 

I am not going to list an assignment-expression, but note that an expression can never be anything, there must be at least one token. This means that an initializer clause can never be anything, and therefore there must always be something between the commas in the function arguments.

+6
source

From the Standard (percussion, pay attention, in particular, to the word trailing ):

8.3.6 Default Arguments [dcl.fct.default]

  • If the initializer clause is specified in the parameter declaration, this initializer clause is used as the default argument. The default arguments will be used in calls where missing arguments are missing.

  • [Example: ad

void point(int = 3, int = 4);

declares a function that can be called with zero, one, or two arguments of type int. It can be called in any of the following ways:

 point(1,2); point(1); point(); 

The last two calls are equivalent to point(1,4) and point(3,4) respectively.

- end of example]

Enjoy the return of your points .;)

+4
source

This is not even a valid C ++ syntax.

+1
source

Probably because it only works for missing final parameters, as indicated by msdn . Try calling the function as follows:

 f(); // should use all default values for arguments f(3); // should use 3 for the first, default for the last two f(2, 5); // uses 2 for first, 5 for second, default for last f(5, 6, 3.4); // does not use any default values 
+1
source

Source: https://habr.com/ru/post/1482229/


All Articles