Bug 52174 - [DR 1423] Implicit conversion of nullptr to bool
Summary: [DR 1423] Implicit conversion of nullptr to bool
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.7.0
: P3 normal
Target Milestone: 5.0
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-02-08 16:44 UTC by xfg
Modified: 2014-06-03 17:50 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2012-02-08 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description xfg 2012-02-08 16:44:47 UTC
Document 'A name for the null pointer: nullptr (revision 4)' states that implicit conversion of nullptr to bool is not allowed. Unfortunately in g++ 4.7.0, this requirement is not fulfilled. See example below.


if (nullptr) //shall return error, implicit conversion to bool not allowed
Comment 1 Jonathan Wakely 2012-02-08 16:53:43 UTC
That document is not the standard, [conv.bool] says "A prvalue of type std::nullptr_t can be converted to a prvalue of type bool; the resulting value is false."
Comment 2 Jonathan Wakely 2012-02-08 16:55:26 UTC
but N.B. http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1423
Comment 3 Jonathan Wakely 2012-02-08 18:05:19 UTC
that DR has been discussed by the committee today and it seems the rule will be changed, so suspending
Comment 4 Daniel Krügler 2012-07-20 18:58:28 UTC
DR 1423 has now reached ready status, 

http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1423

so this seems like a good reason to consider a compiler change to evaluate potential code breakages.

Here some recommended test-cases that should hold:

1)

bool b1 = nullptr; // Is well-formed today, needs to be ill-formed

2)

bool b2(nullptr); // well-formed today and under 1423

3)

template<class T>
T&& make();

template<class T>
void sink(T);

template<class T1, class T2,
  class = decltype(sink<T2>(make<T1>()))
>
auto f(int) -> char(&)[1];

template<class, class>
auto f(...) -> char(&)[2];

static_assert(sizeof(f<decltype(nullptr), bool>(0)) != 1, "");
Comment 5 Paolo Carlini 2012-10-05 19:31:08 UTC
Unsuspending then.
Comment 6 Paolo Carlini 2014-06-03 03:52:12 UTC
Mine.
Comment 7 paolo@gcc.gnu.org 2014-06-03 17:49:08 UTC
Author: paolo
Date: Tue Jun  3 17:48:36 2014
New Revision: 211195

URL: http://gcc.gnu.org/viewcvs?rev=211195&root=gcc&view=rev
Log:
gcc/cp
2014-06-03  Paolo Carlini  <paolo.carlini@oracle.com>

	DR 1423
	PR c++/52174
	* call.c (standard_conversion): Convert nullptr to bool only
	in case of direct-initialization.
	(convert_like_real): Provide informative error message.

gcc/testsuite
2014-06-03  Paolo Carlini  <paolo.carlini@oracle.com>

	DR 1423
	PR c++/52174
	* g++.dg/cpp0x/nullptr31.C: New.
	* g++.dg/cpp0x/sfinae-nullptr1.C: Likewise.
	* g++.dg/cpp0x/nullptr17.C: Update.

libstdc++-v3
2014-06-03  Paolo Carlini  <paolo.carlini@oracle.com>

	DR 1423
	PR c++/52174
	* testsuite/20_util/is_assignable/value.cc: Update.

Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/nullptr31.C
    trunk/gcc/testsuite/g++.dg/cpp0x/sfinae-nullptr1.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/call.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/g++.dg/cpp0x/nullptr17.C
    trunk/libstdc++-v3/ChangeLog
    trunk/libstdc++-v3/testsuite/20_util/is_assignable/value.cc
Comment 8 Paolo Carlini 2014-06-03 17:50:25 UTC
Fixed for 4.10.0.