Jonathon Hill
Jonathon Hill

Reputation: 1057

C++: Safest way to cast boost::optional<type> to type

C++ boost::optional question. How can I cast boost::optional myInt to an int (safely). In my program I have an if statement that checks myInt is indeed initialized, so I want to pass it around in different functions as an int rather then boost::optional for readability purposes. Thanks in advance!

Upvotes: 1

Views: 2258

Answers (1)

4386427
4386427

Reputation: 44274

Can't you just do

boost::optional<int> x;
//....
//....
if (x)
{
    int y = *x;  // or y = x.get();
}

Upvotes: 4

Related Questions