Johnny Shaw
2018-10-06 01:48:32 UTC
Recently Iâve noticed some interpretations of the following standards
resulting in removing of compiler warnings around implicit conversions to a
âboolâ type:
1) C++14 §4.12/1 â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.5), a prvalue of type std::nullptr_t can be
converted to a prvalue of type bool; the resulting value is false.â
2) C99 and C11 §6.3.1.2/1 âWhen any scalar value is converted to _Bool, the
result is 0 if the value compares equal to 0; otherwise, the result is 1.â
Specifically, the VS1017 compiler has removed the following warning
completely:
https://msdn.microsoft.com/en-us/library/b6801kcy.aspx
Over correspondence with Microsoft Iâve noticed it my be due to direct
interpretation of standards. See:
https://developercommunity.visualstudio.com/content/problem/346302/vs2017-158-c-fails-to-warn-when-forcing-int-to-boo.html
https://developercommunity.visualstudio.com/content/problem/349342/visual-studio-2017-158-allows-implicit-conversion.html
This is moving the bar backward a bit, resulting in unintended bugs being
overlooked that would have previously been caught by the compiler.
While there is value in implicit conversion under some circumstances.
Explicit conversion clearly identifies the intent and eliminates mistakes.
Enforcing a standard that requires explicit conversion to bool, at very
least for a return value, will eliminate possible coding errors. The
following are some mundane examples and could be overlooked with given more
complex code:
bool TestFunc(int a)
{
return a;
}
bool TestFuncNotOver100(int a)
{
if (a > 100)
{
return a;
}
return true;
}
bool TestPointerForNull(void* pvX)
{
if (nullptr != pvX)
{
return false;
}
return pvX;
}
At very least requiring a check against a matching type by boolean
operation, for example:
bool TestFunc(int a)
{
return (0 == a);
}
Or for assignment:
int a = 1;
bool bValue = (0 == a);
Moreover, requiring an explicit cast:
bool TestFunc(int a)
{
return static_cast<bool>(a);
}
resulting in removing of compiler warnings around implicit conversions to a
âboolâ type:
1) C++14 §4.12/1 â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.5), a prvalue of type std::nullptr_t can be
converted to a prvalue of type bool; the resulting value is false.â
2) C99 and C11 §6.3.1.2/1 âWhen any scalar value is converted to _Bool, the
result is 0 if the value compares equal to 0; otherwise, the result is 1.â
Specifically, the VS1017 compiler has removed the following warning
completely:
https://msdn.microsoft.com/en-us/library/b6801kcy.aspx
Over correspondence with Microsoft Iâve noticed it my be due to direct
interpretation of standards. See:
https://developercommunity.visualstudio.com/content/problem/346302/vs2017-158-c-fails-to-warn-when-forcing-int-to-boo.html
https://developercommunity.visualstudio.com/content/problem/349342/visual-studio-2017-158-allows-implicit-conversion.html
This is moving the bar backward a bit, resulting in unintended bugs being
overlooked that would have previously been caught by the compiler.
While there is value in implicit conversion under some circumstances.
Explicit conversion clearly identifies the intent and eliminates mistakes.
Enforcing a standard that requires explicit conversion to bool, at very
least for a return value, will eliminate possible coding errors. The
following are some mundane examples and could be overlooked with given more
complex code:
bool TestFunc(int a)
{
return a;
}
bool TestFuncNotOver100(int a)
{
if (a > 100)
{
return a;
}
return true;
}
bool TestPointerForNull(void* pvX)
{
if (nullptr != pvX)
{
return false;
}
return pvX;
}
At very least requiring a check against a matching type by boolean
operation, for example:
bool TestFunc(int a)
{
return (0 == a);
}
Or for assignment:
int a = 1;
bool bValue = (0 == a);
Moreover, requiring an explicit cast:
bool TestFunc(int a)
{
return static_cast<bool>(a);
}
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/5df28d28-a2f4-47cd-9da7-49c91ed6d6f4%40isocpp.org.
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/5df28d28-a2f4-47cd-9da7-49c91ed6d6f4%40isocpp.org.