Bug? std::string promoted to bool???

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Bug? std::string promoted to bool???

U.Mutlu
Why does this compile?
The 2nd par should be a bool not a std::string.
IMO the compiler should generate a warning (actually even an error) about it.

#include <string>

struct TS {};

bool func(const TS& S, const bool f, const std::string s = "")
   {
     //...
     return f;
   }

int main()
   {
     TS S;
     func(S, "X");   // <-- HERE
     return 0;
   }


--
Thx
Uenal



_______________________________________________
help-gplusplus mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-gplusplus
Reply | Threaded
Open this post in threaded view
|

Re: Bug? std::string promoted to bool???

crimaniak@googlemail.com-2
среда, 13 мая 2015 г., 5:47:40 UTC+3 пользователь U.Mutlu написал:
> The 2nd par should be a bool not a std::string.
> IMO the compiler should generate a warning (actually even an error) about it.
...
>      func(S, "X");   // <-- HERE

It is char *, not a std::string.

From docs:

Boolean conversions

Prvalues of integral, floating-point, unscoped enumeration, pointer, and pointer-to-member types can be converted to prvalues of type bool.

The value zero (for integral, floating-point, and unscoped enumeration) and the null pointer and the null pointer-to-member values become false. All other values become true.
_______________________________________________
help-gplusplus mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-gplusplus
Reply | Threaded
Open this post in threaded view
|

Re: Bug? std::string promoted to bool???

U.Mutlu
[hidden email] wrote on 05/13/2015 08:12 PM:

> среда, 13 мая 2015 г., 5:47:40 UTC+3 пользователь U.Mutlu написал:
>> The 2nd par should be a bool not a std::string. IMO the compiler should
>> generate a warning (actually even an error) about it.
> ...
>> func(S, "X");   // <-- HERE
>
> It is char *, not a std::string.
>
> From docs:
>
> Boolean conversions
>
> Prvalues of integral, floating-point, unscoped enumeration, pointer, and
> pointer-to-member types can be converted to prvalues of type bool.
>
> The value zero (for integral, floating-point, and unscoped enumeration) and
> the null pointer and the null pointer-to-member values become false. All
> other values become true.

But this doesn't cover this special case (char*) here, does it?
Whatever the doc says, it is IMO an extremly dangerous construct.
As such, the compiler should at least give a warning.

--
cu
Uenal



_______________________________________________
help-gplusplus mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-gplusplus