I have a C++ class constructor like:

Motors(int p1=0, int p2=0);

However, when I attempt to instantiate it with named parameters like:

Motors motors(p1=123, p2=456);

the Arduino C/C++ compiler gives me the errors:

src/motors.ino:3: error: ‘p1’ was not declared in this scope
src/motors.ino:3: error: ‘p2’ was not declared in this scope

The compiler doesn't seem to support named parameters and thinks I'm referring to variables. I thought named parameters was a pretty widely supported feature, even in C++. What am I doing wrong?


C++ doesn't support named parameters, and I doubt it ever will.

It has strict calling conventions which mean you couldn't re-order the parameters at call-time without adding an unnecessary layer of data duplication/indirection (which would hurt performance). It would also need a significant change to compiler architecture, as variable/parameter names are usually stripped out long before the linker is invoked.

If you want to achieve a similar effect though, you could use comment blocks instead, e.g.:

Motors motors(/*p1*/ 123, /*p2*/ 456);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.