c++: Refine enum direct-list-initialization [CWG2374]

This implements the wording changes of CWG2374, which clarifies the
wording of P0138 to forbid e.g. direct-list-initialization of a scoped
enumeration from a different scoped enumeration.

gcc/cp/ChangeLog:

	DR 2374
	* decl.c (is_direct_enum_init): Check the implicit
	convertibility requirement added by CWG 2374.

gcc/testsuite/ChangeLog:

	DR 2374
	* g++.dg/cpp1z/direct-enum-init2.C: New test.
This commit is contained in:
Patrick Palka 2021-04-23 08:28:58 -04:00
parent 35b2be219f
commit 5f8aed72e7
2 changed files with 15 additions and 1 deletions

View file

@ -6191,7 +6191,13 @@ is_direct_enum_init (tree type, tree init)
&& ENUM_FIXED_UNDERLYING_TYPE_P (type)
&& TREE_CODE (init) == CONSTRUCTOR
&& CONSTRUCTOR_IS_DIRECT_INIT (init)
&& CONSTRUCTOR_NELTS (init) == 1)
&& CONSTRUCTOR_NELTS (init) == 1
/* DR 2374: The single element needs to be implicitly
convertible to the underlying type of the enum. */
&& can_convert_arg (ENUM_UNDERLYING_TYPE (type),
TREE_TYPE (CONSTRUCTOR_ELT (init, 0)->value),
CONSTRUCTOR_ELT (init, 0)->value,
LOOKUP_IMPLICIT, tf_none))
return true;
return false;
}

View file

@ -0,0 +1,8 @@
// DR 2374
// { dg-do compile { target c++17 } }
enum class Orange;
enum class Apple;
extern Orange o;
Apple a{o}; // { dg-error "cannot convert" }