Bug 48450 - [C++0x][SFINAE] Hard errors with static_cast expressions
[C++0x][SFINAE] Hard errors with static_cast expressions
Status: RESOLVED FIXED
Product: gcc
Classification: Unclassified
Component: c++
4.7.0
: P3 normal
: 4.7.0
Assigned To: Not yet assigned to anyone
:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2011-04-05 06:41 UTC by Daniel Krügler
Modified: 2011-04-08 15:11 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Daniel Krügler 2011-04-05 06:41:17 UTC
The following families of expressions give hard compiler errors in a SFINAE context:

     a) any T => abstract class
     b) cv void* => T* (T != void)
     c) cv Base* => Derived*
     d) cv T Derived::* => T Base::*
     e) cv void => T (T is a class type)
     f) Scoped enum => bool

The following code should be well-formed, but is not:

#include <utility> // for declval

template<class To, class From,
 class = decltype(static_cast<To>(std::declval<From>()))
>
char f(int);

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

struct A { virtual ~A() = 0; };
struct B {};
struct D : B {};
enum class SE {};

static_assert(sizeof(f<A, int>(0)) != 1, "Error"); // a
static_assert(sizeof(f<int*, const void*>(0)) != 1, "Error"); // b
static_assert(sizeof(f<D*, const B*>(0)) != 1, "Error"); // c
static_assert(sizeof(f<int B::*, const int D::*>(0)) != 1, "Error"); // d
static_assert(sizeof(f<B, void>(0)) != 1, "Error"); // e
static_assert(sizeof(f<bool, SE>(0)) == 1, "Error"); // f

a) "error: cannot allocate an object of abstract type 'A' because the
following virtual functions are pure within 'A': virtual A::~A()"
b) "error: static_cast from type 'const void*' to type 'int*' casts
away qualifiers"
c) "error: static_cast from type 'const B*' to type 'D*' casts away qualifiers"
d) "error: static_cast from type 'const int D::*' to type 'int B::*'
casts away qualifiers"
e) "error: invalid use of void expression"
f) "error: invalid operands of types 'SE' and 'int' to binary 'operator!='"
Comment 1 Jason Merrill 2011-04-07 21:46:50 UTC
Author: jason
Date: Thu Apr  7 21:46:48 2011
New Revision: 172138

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=172138
Log:
	PR c++/48450
	* c-family/c-common.c (c_common_truthvalue_conversion): Don't ignore
	conversion from C++0x scoped enum.
	* cp/cvt.c (ocp_convert): Handle converting scoped enum to bool.

Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/enum9.C
Modified:
    trunk/gcc/c-family/ChangeLog
    trunk/gcc/c-family/c-common.c
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/cvt.c
    trunk/gcc/testsuite/ChangeLog
Comment 2 Jason Merrill 2011-04-07 21:47:27 UTC
Author: jason
Date: Thu Apr  7 21:47:24 2011
New Revision: 172143

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=172143
Log:
	PR c++/48450
	* tree.c (build_cplus_new, build_aggr_init_expr): Take complain.
	(bot_manip): Adjust.
	* cp-tree.h: Adjust.
	* call.c (convert_like_real, build_cxx_call): Adjust.
	(perform_direct_initialization_if_possible): Adjust.
	* cvt.c (ocp_convert): Adjust.
	* init.c (build_value_init): Adjust.
	* semantics.c (maybe_add_lambda_conv_op): Adjust.
	* typeck.c (unary_complex_lvalue, cp_build_modify_expr): Adjust.
	* typeck2.c (build_functional_cast): Adjust.

Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/call.c
    trunk/gcc/cp/cp-tree.h
    trunk/gcc/cp/cvt.c
    trunk/gcc/cp/init.c
    trunk/gcc/cp/semantics.c
    trunk/gcc/cp/tree.c
    trunk/gcc/cp/typeck.c
    trunk/gcc/cp/typeck2.c
Comment 3 Jason Merrill 2011-04-07 21:47:41 UTC
Author: jason
Date: Thu Apr  7 21:47:38 2011
New Revision: 172145

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=172145
Log:
	PR c++/48450
	* typeck.c (check_for_casting_away_constness): Take complain.
	(build_static_cast_1, build_reinterpret_cast_1): Pass it.
	(build_const_cast_1): Pass it.  Take full complain parm.
	(build_const_cast, cp_build_c_cast): Adjust.

Added:
    trunk/gcc/testsuite/c-c++-common/Wcast-qual-1.c
      - copied, changed from r172144, trunk/gcc/testsuite/gcc.dg/cast-qual-3.c
Removed:
    trunk/gcc/testsuite/g++.dg/warn/Wcast-qual2.C
    trunk/gcc/testsuite/gcc.dg/cast-qual-3.c
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/typeck.c
    trunk/gcc/testsuite/ChangeLog
Comment 4 Jason Merrill 2011-04-07 21:47:48 UTC
Author: jason
Date: Thu Apr  7 21:47:45 2011
New Revision: 172146

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=172146
Log:
	PR c++/48450
	* call.c (resolve_args): Take complain.
	(build_new_function_call, build_operator_new_call): Pass it.
	(build_op_call, build_new_op, build_new_method_call): Pass it.

Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/sfinae9.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/call.c
    trunk/gcc/testsuite/ChangeLog
Comment 5 Jonathan Wakely 2011-04-08 09:15:37 UTC
(In reply to comment #1)
> Author: jason
> Date: Thu Apr  7 21:46:48 2011
> New Revision: 172138
> 
> URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=172138
> Log:
>     PR c++/48450
>     * c-family/c-common.c (c_common_truthvalue_conversion): Don't ignore
>     conversion from C++0x scoped enum.
>     * cp/cvt.c (ocp_convert): Handle converting scoped enum to bool.

Does this fix PR 48107 ?

(I should have CC'd you on that, Jason, sorry)
Comment 6 Jason Merrill 2011-04-08 15:01:54 UTC
Author: jason
Date: Fri Apr  8 15:01:50 2011
New Revision: 172191

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=172191
Log:
	PR c++/48450
	* c-family/c-common.c (c_common_truthvalue_conversion): Don't ignore
	conversion from C++0x scoped enum.
	* cp/cvt.c (ocp_convert): Handle converting scoped enum to bool.

Added:
    branches/gcc-4_6-branch/gcc/testsuite/g++.dg/cpp0x/enum9.C
Modified:
    branches/gcc-4_6-branch/gcc/c-family/ChangeLog
    branches/gcc-4_6-branch/gcc/c-family/c-common.c
    branches/gcc-4_6-branch/gcc/cp/ChangeLog
    branches/gcc-4_6-branch/gcc/cp/cvt.c
    branches/gcc-4_6-branch/gcc/testsuite/ChangeLog
Comment 7 Jason Merrill 2011-04-08 15:11:48 UTC
(In reply to comment #5)
> Does this fix PR 48107 ?

Yes, it does.