Как получить указатель из boost :: необязательный? - PullRequest
0 голосов
/ 02 марта 2020

Я хочу получить указатель, который хранится в моем boost :: необязательно. Есть ли предпочтительный способ сделать это? То, что я получил сейчас, выглядит довольно громоздким (разыскивание и ссылка снова):


// a function I want to call
void processFrame(const Frame* frame) { ... }

// this is how I get my optional
boost::optional<Frame> frame = video.getFrame();

// this is my call so far, which I am not satisfied with (looks strange)
processFrame(frame ? &(*frame) : nullptr);

1 Ответ

1 голос
/ 02 марта 2020

Вы можете использовать get_ptr() метод:

// Returns a pointer to the value if this is initialized, otherwise,
// returns NULL.
// No-throw
pointer_const_type get_ptr() const { return m_initialized ? get_ptr_impl() : 0 ; }
pointer_type       get_ptr()       { return m_initialized ? get_ptr_impl() : 0 ; }

, тогда ваш вызов уменьшается до:

processFrame(frame.get_ptr());

Демо

...