http://coliru.stacked-crooked.com/a/f6bcee72b5c957d1
#include <iostream>
struct CheckDefault {
CheckDefault() : value(false), param_default(true) {}
CheckDefault(bool b) : value(b), param_default(false) {}
bool value;
bool param_default;
};
void func(int param1, CheckDefault s = CheckDefault()) {
if (s.param_default)
std::cout << "with default, ";
else
std::cout << "not default, ";
if (s.value)
std::cout << "and value is true" << std::endl;
else
std::cout << "and value is false" << std::endl;
}
int main() {
func(5);
func(5, false);
func(5, true);
}
The output:
$ ./test2
with default, and value is false
not default, and value is false
not default, and value is true
There needs to be more information than just the single bool so we introduce a second bool and wrap them two together in a new type. Calling the function with an explicit bool triggers the CheckDefault constructor that takes a bool as the only argument. This constructor also initializes param_default to false. Calling without an explicit bool triggers the constructor that takes no arguments. It initializes records s.value with false but sets param_default to true to note that this was a default.
Edited to show inputs for both true and false.
The suggestion by others to use an overload is a good one. To be more explicit, here's what that would look like:
void func(int param1) {
bool b = false;
// do stuff ...
}
void func(int param1, bool b) {
// do stuff ...
}