Reputation: 41
I have a function which accepts an bool as an argument:
void func(bool a)
{
doing something;
}
But when I'm calling the function and passed string to it:
func("false");
Actually it should accept only.
func(false);
It accepts string without any error.
why?
Upvotes: 3
Views: 1482
Reputation: 150
@101010's answer is good. But for the benefit of those with older compilers (which I suspect do not accept that syntax), there is an alternative approach.
First:
A template function declaration with no definition/implementation. That way, if you call it accidentally with func("false")
or func('y')
or func(0)
(an int), the linker will complain. Which will make you look for the problem.
template<typename T> void func(T a); // prototype only
Declare this alongside your real void func(bool a)
function.
If you have this inside a class, you can make the template function/method private, which will make the error into a compiler error (easier to understand, but if the accidental mis-typed call comes from within a class or friend, it will still be a linker error).
Upvotes: 1
Reputation: 42929
According to the C++ standard §4.14/p1 Boolean conversions [conv.bool] (Emphasis Mine):
A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true. For direct-initialization (8.6), a prvalue of type std::nullptr_t can be converted to a prvalue of type bool; the resulting value is false.
That is, according the C++ standard, conversion from a string literal (i.e., pointer) to a boolean is a standard conversion and thus what you're experiencing is perfectly normal/standard language behavior.
You can restrict your function to explicitly accept only bool values by defining a generic overload of your function as deleted:
template<typename T> void func(T&&) = delete;
void func(bool a) {
// do something...
}
Upvotes: 4
Reputation: 3614
Literal string is a char pointer, and pointer will be implicitly converted to bool.
false
true
In this case, the func will receive a true
value from the non-null "false" string , which is const char*
type.
You may need an interpreter function to able to read "false", "0", "no", etc from keyboard typing and convert to bool false which the program can understand.
Upvotes: 5
Reputation: 35154
Pointers can be implicitly converted to bool
; The value is false
, if the pointer is NULL
, and it is true
if the pointer is !NULL
. Hence, since you pass a pointer to char *
-literal "false"
, this pointer is ! NULL
and yields true
. See the following definition in the cpp reference concerning of implicit conversion:
Boolean conversions
A prvalue of integral, floating-point, unscoped enumeration, pointer, and pointer-to-member types can be converted to a prvalue 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
Upvotes: 2