Implicit conversion to bool for std::exeption_ptr

P45 Imminent

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?

Lightness Races in Orbit

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. ;)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Error on implicit cast from std:unique_ptr to bool

From Dev

Boost Optional implicit conversion to bool?

From Dev

Implicit conversion of return expressions to bool

From Dev

C++11 Lambda functions implicit conversion to bool vs. std::function

From Dev

C++11 Lambda functions implicit conversion to bool vs. std::function

From Dev

std::function implicit type conversion

From Dev

Is there an implicit conversion from std::shared_ptr<T> to std::shared_ptr<const T>?

From Dev

C++ Implicit conversion from bool to string

From Dev

Can I define implicit conversion from std::function to std::shared_ptr<MyClass>?

From Dev

Can I define implicit conversion from std::function to std::shared_ptr<MyClass>?

From Dev

Implicit conversion of shared_ptr

From Dev

Why implicit conversion of bool to string isn't an error?

From Dev

No viable conversion from std::function to bool

From Dev

Explicit conversion of std::basic_ios to bool

From Dev

Implicit conversion from int to shared_ptr

From Dev

shared_ptr assignment notation implicit conversion

From Dev

C++, does bool conversion always fall back to implicit conversion to void*?

From Dev

Add implicit conversion from unique_ptr<T> to T*

From Dev

Is it called "implicit casting" or "implicit conversion"?

From Dev

Scala - Implicit conversion to implicit argument

From Dev

Scala implicit conversion of implicit argument

From Dev

No implicit conversion from std::string to std::string_view in C++17 (was in std::experimental::basic_string_view)

From Dev

std::shared_ptr<T>: implicit constructor for rvalue pointer to T

From Dev

Understanding: conversion from long_ptr to bool possible loss of data

From Dev

Implicit conversion to lvalue reference

From Dev

Implicit conversion in the other direction

From Dev

No implicit conversion of Array into String?

From Dev

No implicit conversion of file into string

From Java

Implicit conversion and operator overload

Related Related

  1. 1

    Error on implicit cast from std:unique_ptr to bool

  2. 2

    Boost Optional implicit conversion to bool?

  3. 3

    Implicit conversion of return expressions to bool

  4. 4

    C++11 Lambda functions implicit conversion to bool vs. std::function

  5. 5

    C++11 Lambda functions implicit conversion to bool vs. std::function

  6. 6

    std::function implicit type conversion

  7. 7

    Is there an implicit conversion from std::shared_ptr<T> to std::shared_ptr<const T>?

  8. 8

    C++ Implicit conversion from bool to string

  9. 9

    Can I define implicit conversion from std::function to std::shared_ptr<MyClass>?

  10. 10

    Can I define implicit conversion from std::function to std::shared_ptr<MyClass>?

  11. 11

    Implicit conversion of shared_ptr

  12. 12

    Why implicit conversion of bool to string isn't an error?

  13. 13

    No viable conversion from std::function to bool

  14. 14

    Explicit conversion of std::basic_ios to bool

  15. 15

    Implicit conversion from int to shared_ptr

  16. 16

    shared_ptr assignment notation implicit conversion

  17. 17

    C++, does bool conversion always fall back to implicit conversion to void*?

  18. 18

    Add implicit conversion from unique_ptr<T> to T*

  19. 19

    Is it called "implicit casting" or "implicit conversion"?

  20. 20

    Scala - Implicit conversion to implicit argument

  21. 21

    Scala implicit conversion of implicit argument

  22. 22

    No implicit conversion from std::string to std::string_view in C++17 (was in std::experimental::basic_string_view)

  23. 23

    std::shared_ptr<T>: implicit constructor for rvalue pointer to T

  24. 24

    Understanding: conversion from long_ptr to bool possible loss of data

  25. 25

    Implicit conversion to lvalue reference

  26. 26

    Implicit conversion in the other direction

  27. 27

    No implicit conversion of Array into String?

  28. 28

    No implicit conversion of file into string

  29. 29

    Implicit conversion and operator overload

HotTag

Archive