Мне нравится использовать std :: experimental :: Необязательно в моем коде C ++, но проблема заключается в том, чтобы значение по умолчанию было тем же типом, что и значение опционального. Укажите, есть ли значение или это ошибка, но было бы хорошо, если бы у C ++ только что был результат < /code> тип, как Rust. < /p>
Есть ли такой тип? Почему не повысит реализацию? Может быть, я пойду прочитать реализацию ржавчины, а затем скопирую ее в C ++?// Function either returns a file descriptor for a listening socket or fails
// and returns a nullopt value.
// My issue: error messages are distributed via perror.
std::experimental::optional get_tcp_listener(const char *ip_and_port);
// You can use value_or to handle error, but the error message isn't included!
// I have to write my own error logger that is contained within
// get_tcp_listener. I would really appreciate if it returned the error
// message on failure, rather than an error value.
int fd = get_tcp_listener("127.0.0.1:9123").value_or(-1);
// Rust has a type which does what I'm talking about:
let fd = match get_tcp_listener("127.0.0.1:9123") {
Ok(fd) => fd,
Err(msg) => { log_error(msg); return; },
}
Подробнее здесь: https://stackoverflow.com/questions/321 ... ltt-e-type