Does C++ support named parameters?

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

Leave a Comment