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.