[core] Breaking bugfix in scoped enums

Hi, While creating tests for scoped enums emulation I've fixed a bug which resulted in the enums being convertible to int. This is a potentially breaking change, the code like this will now not compile: BOOST_SCOPED_ENUM_DECLARE_BEGIN(color) { red, green, blue } BOOST_SCOPED_ENUM_DECLARE_END(color) void foo(boost::native_type<color>::type col); color col = color::blue; foo(col); The call to foo() should be done like this: foo(boost::native_value(col)); Previously, the conversion was implicit.

Le 07/06/14 00:41, Andrey Semashev a écrit :
Hi,
While creating tests for scoped enums emulation I've fixed a bug which
Thnaks for fixing this typename EnumType::enum_type native_value(EnumType e) { - return e.native_value_(); + return e.get_native_value_(); }
resulted in the enums being convertible to int. This is a potentially breaking change, the code like this will now not compile:
BOOST_SCOPED_ENUM_DECLARE_BEGIN(color) { red, green, blue } BOOST_SCOPED_ENUM_DECLARE_END(color)
void foo(boost::native_type<color>::type col);
color col = color::blue; foo(col);
The call to foo() should be done like this:
foo(boost::native_value(col));
Previously, the conversion was implicit.
Yes, this is a breaking change. The implicit conversion from EnumType to enum_type was intentional. operator enum_type() const BOOST_NOEXCEPT { return get_native_value_(); } I was expecting two implicit conversions: one from EnumType to enum_type and the other from enum_type to int. Am I missing something in C++98? Vicente

On Saturday 07 June 2014 01:04:28 Vicente J. Botet Escriba wrote:
Le 07/06/14 00:41, Andrey Semashev a écrit :
resulted in the enums being convertible to int. This is a potentially breaking> change, the code like this will now not compile: BOOST_SCOPED_ENUM_DECLARE_BEGIN(color) {
red, green, blue
} BOOST_SCOPED_ENUM_DECLARE_END(color)
void foo(boost::native_type<color>::type col);
color col = color::blue; foo(col);
The call to foo() should be done like this: foo(boost::native_value(col));
Previously, the conversion was implicit.
Yes, this is a breaking change. The implicit conversion from EnumType to enum_type was intentional.
operator enum_type() const BOOST_NOEXCEPT { return get_native_value_(); }
I was expecting two implicit conversions: one from EnumType to enum_type and the other from enum_type to int. Am I missing something in C++98?
I'll have to check later, but scoped_enum_compile_fail_conv_to_int.cpp test fails with clang 3.4 in C++03 mode with this operator.

Vicente J. Botet Escriba wrote:
operator enum_type() const BOOST_NOEXCEPT { return get_native_value_(); }
I was expecting two implicit conversions: one from EnumType to enum_type and the other from enum_type to int. Am I missing something in C++98?
If enum_type is an enum type, its conversion to int is not user-defined and doesn't count.

Le 07/06/14 01:30, Peter Dimov a écrit :
Vicente J. Botet Escriba wrote:
operator enum_type() const BOOST_NOEXCEPT { return get_native_value_(); }
I was expecting two implicit conversions: one from EnumType to enum_type and the other from enum_type to int. Am I missing something in C++98?
If enum_type is an enum type, its conversion to int is not user-defined and doesn't count.
Thanks Peter I don't know to what I was thinking :( An enum (scoped or not) is always implicitly convertible to its underlying type. Andrey, why this implicit conversion must be removed? What is wrong with it? - operator enum_type() const BOOST_NOEXCEPT { return get_native_value_(); } I have added the line you removed and all the test pass. Which case need to remove this implicit conversion? Best, Vicente

On Saturday 07 June 2014 11:02:12 Vicente J. Botet Escriba wrote:
Thanks Peter I don't know to what I was thinking :( An enum (scoped or not) is always implicitly convertible to its underlying type.
Scoped enums must not implicitly convert to integers, be that underlying type or not. See 7.2/9 [dcl.enum].
Andrey, why this implicit conversion must be removed? What is wrong with it?
- operator enum_type() const BOOST_NOEXCEPT { return get_native_value_(); }
I have added the line you removed and all the test pass. Which case need to remove this implicit conversion?
As I said, scoped_enum_compile_fail_conv_to_int.cpp test fails (i.e. it compiles while it shouldn't).

On Sat, Jun 7, 2014 at 5:37 AM, Andrey Semashev
On Saturday 07 June 2014 11:02:12 Vicente J. Botet Escriba wrote:
Thanks Peter I don't know to what I was thinking :( An enum (scoped or not) is always implicitly convertible to its underlying type.
Scoped enums must not implicitly convert to integers, be that underlying type or not. See 7.2/9 [dcl.enum].
Right. The File System TS made that mistake, and there was an issue filed. Presumably Boost.Filesystem docs are also wrong. It would be very helpful if the scoped enum emulation detected an implicit conversion attempt as an error. Thanks, --Beman

Le 07/06/14 11:37, Andrey Semashev a écrit :
On Saturday 07 June 2014 11:02:12 Vicente J. Botet Escriba wrote:
Thanks Peter I don't know to what I was thinking :( An enum (scoped or not) is always implicitly convertible to its underlying type. Scoped enums must not implicitly convert to integers, be that underlying type or not. See 7.2/9 [dcl.enum]. Right. Sorry for the noise.
Andrey, why this implicit conversion must be removed? What is wrong with it?
- operator enum_type() const BOOST_NOEXCEPT { return get_native_value_(); }
I have added the line you removed and all the test pass. Which case need to remove this implicit conversion? As I said, scoped_enum_compile_fail_conv_to_int.cpp test fails (i.e. it compiles while it shouldn't).
Sorry, I missed it. Thanks for all, the fixes, and the tests. Vicente
participants (4)
-
Andrey Semashev
-
Beman Dawes
-
Peter Dimov
-
Vicente J. Botet Escriba