Use case comparison for std::optional vs default argument

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 the defaultSettings.

  • in the second case, the defaultSettings are defined at the level of the code which calls the printFunction, which is here the Printer 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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật