C++ default arguments out of order?

  • A+
Category:Languages

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++?


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

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: