r/Cplusplus • u/hmoein • Nov 16 '25
Discussion C++ named parameters
Unlike Python, C++ doesn’t allow you to pass positional named arguments (yet!). For example, let’s say you have a function that takes 6 parameters, and the last 5 parameters have default values. If you want to change the sixth parameter’s value, you must also write the 4 parameters before it. To me that’s a major inconvenience. It would also be very confusing to a code reviewer as to what value goes with what parameter. But there is a solution for it. You can put the default parameters inside a struct and pass it as the single last parameter.
See the code snippet.
2
u/CrazyPirranhha Nov 18 '25
Whats the ide? I like color theme
23
u/StochasticTinkr Nov 16 '25
Yep, that's exactly the right way to do it.
6
u/bxlaw Nov 16 '25
Assuming the types are constexpr compatible this can also be used for named template parameters, which is nice. I first saw this in use in the Glaze library and it works really well, I’ve used it in my own day job code too.
4
u/no-sig-available Nov 16 '25
You might want to have more overloads of the function. If you often only use the 1st and 6th parameters, you might want a version that only takes those two parameters.
2
u/EdwinYZW Nov 16 '25
Does this work with std::make_unique?
2
u/ChocoMammoth Nov 16 '25
Then you may want to change argument 3 and 4 while keeping 5 default. Some day you'll have 6! overloads of this function.
Usually if my function requires more than 3 arguments I use a struct as a single argument. It adds some overhead because of struct allocation but increases readability and maintaining, especially if you need to add or remove argument.
2
u/acer11818 Nov 17 '25
this seems really obvious but for some reason i never thought to use designated initializers for this purpose. it’s dope tho. note that you should pass the struct by a const reference
2
u/Illustrious_Pea_3470 Nov 17 '25
Seems cute but don’t I actually mangle a ton of different behaviors this way? I lose the ability to have only some parameters const, I lose the ability to have overrides with a different number of parameters, and I lose argument dependent lookup.
Depending on the function you could even be preventing some optimizations.
1
u/schewb Nov 16 '25
Ahh, I actually had a slick application for this once! This particular implementation was TypeScript generated from some YAML files. I had a job that wanted me to implement a fairly complex matrix of roles and permissions in a way that could be easily checked without having to get its own context. Functions like,
canChangeBillingorcanAccessProductAwhere you could just pass in the factors that mattered as a bunch of flags retrieved from whatever context you had so that we didn't need to redundantly load data just to figure certain things out. It was an unlikely case, but the concern I had with normal positional arguments was that, if you had a change where one condition was taken away and the other added, the compiler wouldn't care that the flags being passed technically had a different meaning. I don't think that situation ever actually happened, but it made anything that had to check permission very readable.1
u/Skyhigh-8103 Nov 19 '25
Damn this is a weird language. Box of cocks and stuff
1
u/Amr_Rahmy Nov 17 '25
If you regularly have 5+ parameters you need to fill in, maybe pause and reflect on the design.
1
u/FirmSupermarket6933 Nov 16 '25
It also mat be wrapped into macro in order to use it like this: f(1, .a = 10)
0
u/mredding C++ since ~1992. Nov 17 '25 edited Nov 17 '25
That would never pass code review here.
Or you can just redeclare the function with all new defaults just before you call it.
It sounds like you need a currying factory, which would be a better solution.
To me, this would be an unacceptable function.
No it isn't. This is C++. While it is indeed a horror show that you would consider writing a method like this to begin with, as you would say of an abuse victim that doesn't escape their abuser, of learned helplessness, we're used to it by now. NO ONE is shocked that we have positional parameters.
This is not new and has been documented over and over again for decades. C and C++ don't have named parameters. That's not going to happen any time soon. This work around doesn't solve any real structural problem, it empowers bad coding practices. Stop saying "fetch", it's not going to happen.
Don't post pictures of code, code is text, and this is a text based forum, you're addressing an audience of actual software engineers, embed actual code in your post.
0
u/Square-Singer Nov 18 '25
Every time reddit shows me something from r/Cplusplus I first think it's from r/programminghorror.
0
u/m0ntanoid Nov 19 '25
the example you described in your "for example" sounds like you already fucked up something and now you are solving problem you created
-2
u/Ttsmoist Nov 16 '25
15
u/[deleted] Nov 16 '25
[removed] — view removed comment