Wanted your opinion on which way will be better. Below is a sample code where I have used std::optional in the argument of a lambda and the lambda itself is an argument of the class constructor.
OPTION 1
struct PrintSettings {
bool is_double_sided;
bool is_color;
int num_copies;
};
class Printer {
public:
Printer(std::function<void(std::optional<PrintSettings>)>&& printFunction)
: printFunction_(std::move(printFunction)) {
}
void print() {
printFunction_(std::nullopt);
}
void printWithSettings(const PrintSettings& settings) {
printFunction_(settings);
}
private:
std::function<void(std::optional<PrintSettings>)> printFunction_;
};
int main() {
const PrintSettings defaultSettings{false, true, 1};
auto printer = std::make_unique<Printer>(
[defaultSettings] (std::optional<PrintSettings> settings) {
const PrintSettings printSettings = settings.value_or(defaultSettings);
std::cout << "Double sided: " << printSettings.is_double_sided << "n";
std::cout << "Color: " << printSettings.is_color << "n";
std::cout << "Number of copies: " << printSettings.num_copies << "n";
});
printer->print();
const PrintSettings someSettings{true, false, 2};
printer->printWithSettings(someSettings);
return 0;
}
Now I understand that instead of using std::optional I can directly put the argument as non-optional in the lambda and then from the function print()(which takes no settings) i can create a default settings argument which will be static const and pass that as the lambda argument as shown below:-
OPTION 2
struct PrintSettings {
bool is_double_sided;
bool is_color;
int num_copies;
};
class Printer {
public:
Printer(std::function<void(const PrintSettings&)>&& printFunction)
: printFunction_(std::move(printFunction)) {
}
void print() {
static const PrintSettings defaultSettings{false, true, 1};
printFunction_(defaultSettings);
}
void printWithSettings(const PrintSettings& settings) {
printFunction_(settings);
}
private:
std::function<void(const PrintSettings&)> printFunction_;
};
int main() {
auto printer = std::make_unique<Printer>(
[] (const PrintSettings& settings) {
std::cout << "Double sided: " << settings.is_double_sided << "n";
std::cout << "Color: " << settings.is_color << "n";
std::cout << "Number of copies: " << settings.num_copies << "n";
});
printer->print();
const PrintSettings someSettings{true, false, 2};
printer->printWithSettings(someSettings);
return 0;
}
I wanted to ask which option will be better in terms of complexities and readability. I understand there is a boolean overhead to std::optional as well.
Please note that printWithSettings() is a newer version of print() and we are keeping print() only for backward compatibility.
1
In your example, the location in code where the default value “lives” is quite different:
-
in the first case, the
printFunction
itself (or the code which composes this function) has to know about thedefaultSettings
. -
in the second case, the
defaultSettings
are defined at the level of the code which calls theprintFunction
, which is here thePrinter
class.
In this simplified example, this may appear not as a huge difference. In a real world program, however, the printFunction
and the Printer
class may live in completely different layers or may be under maintenance of completely different people or organizations. Hence the decision criteria should be in which layer or by whom you want the defaultSettings
to be maintained.
“optional” should be used for things that are genuinely not there, and not for default values. So if it is possible that there are no print settings, make it optional. If you decided to use default values, you can put defaults into your parameter list, are pass them directly.
2