0

I'm writing some code that necessitates my caching an exception.

Please consider

int main() { std::exception_ptr ex; bool b = ex; } 

This doesn't compile due to ex not being convertible to a bool type. My current workaround is to write

bool b = !!ex; 

or even

bool b = ex ? true : false; 

The first way is ugly, the second one a tautology surely. I'm starting to blame the compiler (MSVC2015). Two things:

  1. Is there a better way of checking if ex has been set to an exception?

  2. (Related) Do I need to initialise ex in some way?

5
  • @Downvoter: I'm sorry if this question offends you. If there is a way I can appease you then do please share your wisdom. Commented May 2, 2017 at 15:17
  • Have you read the documentation? en.cppreference.com/w/cpp/error/exception_ptr Commented May 2, 2017 at 15:17
  • Yes I have. But I don't understand why using !! works, along with the conditional. What am I missing? Commented May 2, 2017 at 15:18
  • 2
    "A default-constructed std::exception_ptr is a null pointer; it does not point to an exception object." from @NathanOliver link addresses (2) in an obvious way. Come on, you're a 3k user, surely you can read technical stuff by now? Minus one for that. Otherwise the question is a good one IMHO. Commented May 2, 2017 at 15:19
  • You can use bool b(ex); or bool b{ex}; if you really want to store that result in bool. Commented May 2, 2017 at 15:20

2 Answers 2

2

Read the documentation.

The implicit conversion is prohibited, but an explicit one is not.

std::exception_ptr is not implicitly convertible to any arithmetic, enumeration, or pointer type. It is contextually convertible to bool, and will evaluate to false if it is null, true otherwise.

Hence it works when you explicitly convert the expression, but not when you attempt to do so implicitly, i.e. in the bool copy-initialization.

A better solution is to initialise the bool directly:

bool b{ex}; 

Your P45 is in the post; hopefully you'll consult documentation in your next job. ;)

Sign up to request clarification or add additional context in comments.

2 Comments

Just to clarify, the syntax bool b{ex} in this case is the same as bool b(ex) and even bool b = static_cast<bool>(ex);. At their core all of them invoke the explicit operator bool() conversion operator on the exception_ptr class.
@Johannes: Same result. Technically not equivalent functionality.
1

The type std::exception_ptr is a nullable, which means that when it is default constructed as you have done it is a null value by default. Because this is a typedef and is not necessarily a raw pointer, it can not be assigned to a bool directly as this would be implicitly converting the type.

You can check to see if it has been set as you have done, or you can do so a bit more cleanly by using nullptr, or explicitly casting it to a boolean:

std::exception_ptr e; bool isNull = false; if(e == nullptr) isNull = true; 

Explicit casting:

std::exception_ptr e; bool isNull = static_cast<bool>(e); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.