Does C++ support default arguments to a function?

Can I declare a function as
1
2
3
4
5
6
bool handshake(uint64_t       slaveAddress,
	       unsigned long* timeData   = timeData,
	       char*          testData   = testData,
	       char*          testNumber = testNumber,
	       int*           ackData    = ackData,
	       bool*          newData    = newData);


and then call it as
 
handshake(slave0);
Not like this (parameters aren't allowed as part of the initializer unless they appear in an unevaluated context), but in general yes. Parameters with default values must come last. Except for parameter packs.
The details are a bit more complicated. See:
http://en.cppreference.com/w/cpp/language/default_arguments

If you need to bind "default" function parameters that are out-of-order or that otherwise violate the requirements, use std::bind or (preferably, IMO) bind free variables using a closure.
Last edited on
Topic archived. No new replies allowed.