C++ Implicit conversion from bool to string

Simmovation

I have the following code, which compiles in Visual C++ 2012.

#include <string>

void func(std::string str)
{
}

void my_func()
{
    func(false);
}

The boolean 'false' is implicity passed into the string constructor

string(const char* _Ptr)

And then the pointer is null (because false = 0). Why does this compile, and should it compile according to the C++11 standard?

chris

MSVC is mistakenly treating false as a null pointer constant. However, according to N4140, §4.10 [conv.ptr]/1 (emphasis mine):

A null pointer constant is an integer literal with value zero or a prvalue of type std::nullptr_t. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of object pointer or function pointer type.

The wording changed a bit from C++11, and you can find that discussion here. The verdict there was that it was an error in C++11 as well.

For visibility, TartanLlama provided the definition of "integer literal" below, according to [lex.icon]/1:

An integer literal is a sequence of digits that has no period or exponent part, with optional separating single quotes that are ignored when determining its value.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C# implicit conversion and conversion from Object

From Dev

C# implicit conversion and conversion from Object

From Dev

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

From Dev

C++ constructors and implicit string conversion

From Dev

c++ implicit type conversion string -> int?

From Dev

Boost Optional implicit conversion to bool?

From Dev

Implicit conversion of return expressions to bool

From Dev

C++ Why use an implicit conversion from (std::string) to (void) type?

From Dev

No implicit conversion of Array into String?

From Dev

No implicit conversion of file into string

From Dev

No implicit conversion of Array into String?

From Dev

implicit conversion string to integer

From Dev

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

From Dev

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

From Dev

Implicit conversion from null

From Dev

Implicit conversion to bool for std::exeption_ptr

From Dev

c = a + b and implicit conversion

From Dev

C implicit conversion?

From Dev

c = a + b and implicit conversion

From Dev

Implicit data conversion from string to int in sql server

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

implicit conversion of vector from one type to another c++

From Dev

No implicit conversion of HTTParty::Response into String

From Dev

no implicit conversion of Hash into String (TypeError)

From Dev

No implicit conversion of String into Integer (TypeError)?

From Dev

Implicit conversion works for symbol but not string

From Dev

no implicit conversion of nil into String error

From Dev

Rails: no implicit conversion of nil into String

Related Related

  1. 1

    C# implicit conversion and conversion from Object

  2. 2

    C# implicit conversion and conversion from Object

  3. 3

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

  4. 4

    C++ constructors and implicit string conversion

  5. 5

    c++ implicit type conversion string -> int?

  6. 6

    Boost Optional implicit conversion to bool?

  7. 7

    Implicit conversion of return expressions to bool

  8. 8

    C++ Why use an implicit conversion from (std::string) to (void) type?

  9. 9

    No implicit conversion of Array into String?

  10. 10

    No implicit conversion of file into string

  11. 11

    No implicit conversion of Array into String?

  12. 12

    implicit conversion string to integer

  13. 13

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

  14. 14

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

  15. 15

    Implicit conversion from null

  16. 16

    Implicit conversion to bool for std::exeption_ptr

  17. 17

    c = a + b and implicit conversion

  18. 18

    C implicit conversion?

  19. 19

    c = a + b and implicit conversion

  20. 20

    Implicit data conversion from string to int in sql server

  21. 21

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

  22. 22

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

  23. 23

    implicit conversion of vector from one type to another c++

  24. 24

    No implicit conversion of HTTParty::Response into String

  25. 25

    no implicit conversion of Hash into String (TypeError)

  26. 26

    No implicit conversion of String into Integer (TypeError)?

  27. 27

    Implicit conversion works for symbol but not string

  28. 28

    no implicit conversion of nil into String error

  29. 29

    Rails: no implicit conversion of nil into String

HotTag

Archive