Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can we use the Indirection/Dereferencing (*) operator on a boost::optional<T> containing boost::none?

Tags:

c++

boost

The following lines will not throw an exception at run-time:

    boost::optional<double> dummy(boost::none);
    double testValue = *dummy;

Is there a reason why boost developers have decided to not throw when using the indirection operator (*) on a boost::optional containing boost::none ? For reference, I get random values in testValue in the code above as it is probably assigning random unitialised parts of memory.

This can lead to silent errors as you are responsible of checking the value before to use it.

like image 843
BlueTrin Avatar asked Aug 26 '14 09:08

BlueTrin


1 Answers

I can think of 2:

  • Consistency -- basic types don't check this.
  • Speed. Dereferencing is a small and fast operation so adding a check can incur a lot of overhead, proportionally.

It is the same case as for array bounds checking: nice to have, but expensive.

like image 58
Germán Diago Avatar answered Oct 20 '22 04:10

Germán Diago