Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why C++ doesn't support named parameter [closed]

Previously, I worked with python. In Python I used named parameter(keyword argument) for function calls. Wikipedia page about named parameter tells that C++ doesn't support it. Why C++ doesn't support named parameter?. Does it support in future version of C++ standard?

like image 566
Jobin Avatar asked Jun 28 '16 12:06

Jobin


People also ask

Does c++ support named arguments?

In Python I used named parameters (keyword argument) for function calls. The Wikipedia page about named parameters tells that C++ doesn't support it.

Why is it a good idea to use named parameters instead of the syntax?

With named parameters, it is usually possible to provide the values in any arbitrary order, since the name attached to each value identifies its purpose. This reduces the connascence between parts of the program.

Which is the parameter name?

Parameter names identify the local or global parameters that are defined by you or that are specified by the SRC software. The parameter name is a string of alphanumeric characters starting with a letter that does not contain spaces or special characters.


1 Answers

Why C++ doesn't support named parameter

Because such feature has not been introduced to the standard. The feature didn't (and doesn't) exist in C either, which is what C++ was originally based on.

Does it support in future version of C++ standard?

Maybe. A proposal has been written for it. It depends on whether the proposal is voted into the standard.


Update: The proposal has been rejected.

A fundamental problem in C++ is that the names of the parameters in function declarations aren't significant, and following program is well-defined:

void foo(int x, int y); void foo(int y, int x);   // re-declaration of the same function void foo(int, int);       // parameter names are optional void foo(int a, int b) {} // definition of the same function 

If named parameters were introduced to the language, then what parameters would be passed here?

foo(x=42, b=42); 

Named parameters require significantly different, and backwards incompatible system of parameter passing.


You can emulate named parameters by using a single parameter of class type:

struct args {     int   a = 42;     float b = 3.14; };  void foo(args);  // usage args a{}; a.b = 10.1; foo(a); // or in C++20 foo({.b = 10.1}); 
like image 59
eerorika Avatar answered Sep 19 '22 10:09

eerorika