Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ default arguments out of order?

Tags:

c++

suppose I have:

void f(bool option1 = false, bool option2 =false, bool option3 = false) 
{
     ... 
}

And I want to call:

f(option2=true);

Is this possible in C++?

like image 925
Chris Avatar asked Nov 29 '22 22:11

Chris


1 Answers

It is not possible to invoke a function in the manner you suggested in C++. You can emulate named parameters via metaprogramming or simply pass a struct to your function. E.g.

struct options
{
    bool option0{false};
    bool option1{false};
    bool option2{false};
}; 

void f(options opts = {});

C++11 usage:

options opts;
opts.option2 = true;
f(opts);

C++2a usage:

f({.option2=true});
like image 172
Vittorio Romeo Avatar answered Dec 05 '22 20:12

Vittorio Romeo