Previously, I worked with Python. 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 doesn't C++ support named parameters? Will it support it in a future version of the C++ standard?
Asked
Active
Viewed 3.4k times
57
-
5[FYI] There is a proposal: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4172.htm – NathanOliver Jun 28 '16 at 12:56
-
1There's been at least one proposal for them. You can try to look into why it isn't part of the standard. – chris Jun 28 '16 at 12:56
-
2It's not about programming, but about a decision. – nbro Jun 28 '16 at 12:56
-
Even Fortran introduced [named parameters](https://rosettacode.org/wiki/Named_parameters#Fortran) with Fortran 95. There is some discussion on [Reddit](https://www.reddit.com/r/cpp/comments/5mdes5/what_happened_to_the_named_parameter_proposal/) about [proposal n4172](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4172.htm), it seems that the reason to abandon it is that function declarations are perfectly valid without using parameter names at all (e.g. `void foo(int, int);`) and as a consequence different implementations of the same library (STL) may use different parameter names. – Andre Holzner Sep 02 '18 at 18:29
-
You can use third-party libraries, which have been mentioned before, or use [Clang Tidy's bugprone-argument-comment](https://clang.llvm.org/extra/clang-tidy/checks/bugprone-argument-comment.html). – Daniel H Dec 02 '18 at 21:12
-
I think the main problem is that function parameter names are not stored in object files. It would not be possible to match parameter names in a function call with a function definition if the two are in separate translation units. – Anonymous1847 Sep 18 '20 at 20:13
-
1@Anonymous1847 hmmm idk, maybe I'm missing something but seems it would not be too hard to resolve fully at compile time. Current ordered argument must somewhere be represented as an indexed list of types. Map a compile-time enum onto those. then the compiler can rearrange named arguments by index according to the constant value. that should allow simple type deduction and avoid any changes to compiled code. effectively working as an alias – That Realty Programmer Guy Aug 16 '21 at 10:41
-
@PaulR it really should be a deal breaker in C++ more than it is. Anything that increases syntactic sugar while also allowing to push more logic out of the runtime is, traditionally, given excessive amounts of attention. Which I have always been fond of – That Realty Programmer Guy Aug 16 '21 at 10:45
-
This question was referenced in meta post *[My question gets completely trashed within hours, while an identical question from 6 years ago gets 50 upvotes. What does it say about this community?](https://meta.stackoverflow.com/questions/420042/my-question-gets-completely-trashed-within-hours-while-an-identical-question-fr)* (unlikely to survive for long—the meta question will be automatically deleted within a few weeks unless an answer is posted). – Peter Mortensen Aug 25 '22 at 14:02
1 Answers
73
Does C++ support named parameters?
No, because this 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.
Will it support it in a future version of the C++ standard?
A proposal was written for it. But the proposal was 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});
-
9Here's what [this page](http://cplusplus.github.io/EWG/ewg-closed.html#150) has to say regarding the proposal: `Discussed in Urbana. EWG found various problems with the proposed approach, and didn't think it's feasible to try solving the problem, as it has been tried many times and every time it has failed.`. – Ruslan May 28 '17 at 06:13
-
1[CppCon 2018 Talk "Named Arguments from Scratch”](https://youtu.be/Grveezn0zhU) by Richard Powell might be useful. – Benjamin Buch Nov 13 '18 at 22:03
-
6i like this feature, it makes the code clear, still no such feature in c++17 – Dee May 16 '19 at 22:58
-
They are supported now: https://pdimov.github.io/blog/2020/09/07/named-parameters-in-c20 Also, you can sort of achieve it either with function chaining (my preference): https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Named_Parameter or "strong types": https://www.fluentcpp.com/2018/12/14/named-arguments-cpp – Andrew Feb 04 '21 at 05:17
-
Here's a working example of the actually supported approach in c++ 20: https://ideone.com/YbX6he (Output: `6 7 8`) – Andrew Feb 04 '21 at 05:24
-
1@Andrew `They are supported now` No they aren't. From the article that you linked: `C++ is obviously not such a language and there have been numerous proposals to rectify this omission, unfortunately none of them successful.` Passing all arguments as a single class has always been a workaround to achieve similar goal. With designated initialisers it has become a very close emulation of true named parameters. – eerorika Feb 04 '21 at 09:23
-
@eerorika They're real named parameters, you just have to put them in order and use a `struct` on the receiving end. I do the exact same thing in JavaScript, using objects as parameters. It's very convenient and this serves the overall purpose and function of named parameters. – Andrew Feb 04 '21 at 14:21
-
5@Andrew `They're real named parameters` No they aren't. They're really member variables. `It's very convenient` While true, that doesn't make them named parameters. `this serves the overall purpose and function of named parameters.` Only some of it. You can use the single class argument to make your own functions emulate named parameters, but that doesn't let you pass named parameters to functions that weren't declared in that way. – eerorika Feb 04 '21 at 14:29
-
@eerorika They're as named parameters as named parameters get and you're wrong. – Andrew Feb 05 '21 at 20:07
-
@Andrew Named parameters would allow `std::sort(first: c.begin(), last: c.end());`. Designated initialisers don't allow that – Caleth Nov 17 '21 at 10:49
-
@Caleth As I said: "They're as named parameters as named parameters get... [in c++]" – Andrew Nov 17 '21 at 19:13
-
@Andrew we are not disputing that they are a workaround. We are disputing that they are equivalent to the feature that was rejected – Caleth Nov 17 '21 at 20:12
-
@Caleth As am I. They're as named parameters-y as fits the c++-world/level of programming. Your example, for instance, would require a total overhaul of the language. This is a foolish comparison; it's comparable to trying to rid the world of gravity. First you'd have to recreate the laws of the universe. To be clear: I'm not saying it will never happen; I am saying that it would take a major overhaul of the language to make it happen. So this is as good as it gets unless and until then. – Andrew Nov 18 '21 at 02:49
-
you can use my implementation: github.com/niXman/tiny-args it allows to implement any functions uses named args: `func(group.a = 3, group.b = 4);` or `func(group.b = 4, group.a = 3);` even with default args. – niXman Jan 21 '23 at 18:29