Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

6 views
Skip to first unread message

U.Mutlu

unread,
May 12, 2015, 10:47:40 PM5/12/15
to help-gp...@gnu.org
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



crim...@gmail.com

unread,
May 13, 2015, 2:12:40 PM5/13/15
to
среда, 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.

U.Mutlu

unread,
May 13, 2015, 8:22:00 PM5/13/15
to help-gp...@gnu.org
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



0 new messages