1

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?

asked Mar 7, 2015 at 19:01

1 Answer 1

5

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);
answered Mar 7, 2015 at 19:17

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.