Return type std::optional>

前端 未结 2 1086
盖世英雄少女心
盖世英雄少女心 2021-02-19 21:04

I have a situation where a function must return a value taken from a table. A cell in this table (let\'s assume the table just works...) may contain a value, or it might not. T

2条回答
  •  半阙折子戏
    2021-02-19 21:33

    I would consider this to be a useful use of std::monostate. Specifically, variant. monostate is useful for cases where a variant may not contain a value.

    The nice thing about using an actual type rather than optional is that visitation works normally on it. You can write a functor that can take a monostate parameter, thus allowing you to use visit for even "empty" variants.

提交回复
热议问题