mardi 2 mai 2017

Implicit conversion to bool for std::exeption_ptr

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?

Aucun commentaire:

Enregistrer un commentaire