Implicit conversion of return expressions to bool

choeger

When trying to build some legacy code (with a recent version of boost), I stumbled upon the following problem:

#include <boost/scoped_array.hpp>

bool foo(const boost::scoped_array<int> bar) {
    return bar;
}

bool foo2(const boost::scoped_array<int> bar) {
    const bool r = bar;
    return r;
}

bool foo3(const boost::scoped_array<int> bar) {
    return bar ? true : false;
}

The above source will not compile. Both foo and foo2 are erroneous. To be precise, the expected implicit conversion from scoped_array to bool is not allowed:

➜  /tmp clang++ --std=c++14 testconversion.cpp -o testconversion.o
testconversion.cpp:4:12: error: no viable conversion from returned value of type 'const boost::scoped_array<int>' to function return type 'bool'
    return bar;
           ^~~
testconversion.cpp:8:16: error: no viable conversion from 'const boost::scoped_array<int>' to 'const bool'
    const bool r = bar;

This brings up two questions:

  1. Why are foo and foo2 not valid? the reference explicitly mentions:

    when initializing a new object of type T2, including return statement in a function returning T2;

  2. When was that legal. The legacy code definitely used to build with boost 1.48.0. Was there
    1. a change in the boost libraries
    2. a change to the language/compilers
Akira

Look inside the documentation of boost::scoped_array:

operator unspecified-bool-type () const; // never throws

Returns an unspecified value that, when used in boolean contexts, is equivalent to get() != 0.

You have to use static_cast to convert your boost::scoped_array to bool:

bool foo(const boost::scoped_array<int>& bar) {
    return static_cast<bool>(bar);
}

bool foo2(const boost::scoped_array<int>& bar) {
    const bool r = static_cast<bool>(bar);
    return r;
}

Maybe just a typo but in your example you passing boost::scoped_array by value. It has no copy constructor, so passing it by value will also cause errors.


It works with Boost 1.48.0 because the operator_bool.hpp was different in that version. In Boost 1.64.0 the same header contains explicit operator bool () const; which prevenst you to copy-initialize a bool from a boost::scoped_array instance when you are compiling it with -std=c++14 (or -std=c++11). Your code will work with Boost 1.64.0 too if you are compiling it with -std=c++98.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Boost Optional implicit conversion to bool?

From Java

Implicit conversion not allowed on return

From Dev

Implicit conversion not allowed on return

From Dev

C++ Implicit conversion from bool to string

From Dev

Implicit conversion to bool for std::exeption_ptr

From Dev

"Cannot be determined because there is no implicit conversion" with ternery if return

From Dev

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

From Dev

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

From Dev

Three-way operator <=> return struct with implicit conversion function

From Dev

What happens first, implicit conversion to return value or destruction of local variable?

From Dev

What happens first, implicit conversion to return value or destruction of local variable?

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

Uniform initialization in return statement and explicit conversion operator to bool

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

Lambda expressions and implicit typing

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

From Dev

Second order implicit conversion

From Dev

Implicit conversion sequence

From Dev

Implicit conversion for generic type?

From Dev

Implicit conversion for multiple parameters

From Dev

Swift implicit conversion of type

From Dev

Implicit conversion with operator

Related Related

  1. 1

    Boost Optional implicit conversion to bool?

  2. 2

    Implicit conversion not allowed on return

  3. 3

    Implicit conversion not allowed on return

  4. 4

    C++ Implicit conversion from bool to string

  5. 5

    Implicit conversion to bool for std::exeption_ptr

  6. 6

    "Cannot be determined because there is no implicit conversion" with ternery if return

  7. 7

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

  8. 8

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

  9. 9

    Three-way operator <=> return struct with implicit conversion function

  10. 10

    What happens first, implicit conversion to return value or destruction of local variable?

  11. 11

    What happens first, implicit conversion to return value or destruction of local variable?

  12. 12

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

  13. 13

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

  14. 14

    Uniform initialization in return statement and explicit conversion operator to bool

  15. 15

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

  16. 16

    Scala - Implicit conversion to implicit argument

  17. 17

    Scala implicit conversion of implicit argument

  18. 18

    Lambda expressions and implicit typing

  19. 19

    Implicit conversion to lvalue reference

  20. 20

    Implicit conversion in the other direction

  21. 21

    No implicit conversion of Array into String?

  22. 22

    No implicit conversion of file into string

  23. 23

    Implicit conversion and operator overload

  24. 24

    Second order implicit conversion

  25. 25

    Implicit conversion sequence

  26. 26

    Implicit conversion for generic type?

  27. 27

    Implicit conversion for multiple parameters

  28. 28

    Swift implicit conversion of type

  29. 29

    Implicit conversion with operator

HotTag

Archive