2014-06-12 02:10:10 +04:00
[/
/ Copyright (c) 2009 Beman Dawes
2014-06-12 03:22:05 +04:00
/ Copyright (c) 2011-2012 Vicente J. Botet Escriba
/ Copyright (c) 2012 Anthony Williams
2014-06-12 02:10:10 +04:00
/ Copyright (c) 2014 Andrey Semashev
/
/ Distributed under the Boost Software License, Version 1.0. (See accompanying
/ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
/]
2014-06-05 10:48:21 -07:00
[section:scoped_enum scoped_enum]
2014-06-12 00:15:52 +04:00
[simplesect Authors]
2014-06-05 10:48:21 -07:00
* Beman Dawes
* Vicente J. Botet Escriba
2014-06-06 12:25:37 +04:00
* Anthony Williams
2014-06-05 10:48:21 -07:00
2014-06-12 00:15:52 +04:00
[endsimplesect]
2014-06-05 10:48:21 -07:00
[section Overview]
2014-06-05 00:19:04 +04:00
The `boost/core/scoped_enum.hpp` header contains a number of macros that can be used to generate
C++11 scoped enums (7.2 \[dcl.enum\]) if the feature is supported by the compiler, otherwise emulate
it with C++03 constructs. The `BOOST_NO_CXX11_SCOPED_ENUMS` macro from Boost.Config is used to detect
the feature support in the compiler.
Some of the enumerations defined in the standard library are scoped enums.
enum class future_errc
{
broken_promise,
future_already_retrieved,
promise_already_satisfied,
no_state
};
The user can portably declare such enumeration as follows:
BOOST_SCOPED_ENUM_DECLARE_BEGIN(future_errc)
{
broken_promise,
future_already_retrieved,
promise_already_satisfied,
no_state
}
BOOST_SCOPED_ENUM_DECLARE_END(future_errc)
2022-09-19 12:53:00 +03:00
These macros allow using `future_errc` in almost all the cases as a scoped enum.
2014-06-05 00:19:04 +04:00
future_errc ev = future_errc::no_state;
It is possible to specify the underlying type of the enumeration:
BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(future_errc, unsigned int)
{
broken_promise,
future_already_retrieved,
promise_already_satisfied,
no_state
}
BOOST_SCOPED_ENUM_DECLARE_END(future_errc)
The enumeration supports explicit conversion from the underlying type.
The enumeration can be forward declared:
BOOST_SCOPED_ENUM_FORWARD_DECLARE(future_errc);
2019-04-23 02:00:26 +02:00
There are however some limitations. The emulated scoped enum is not a C++ enum, so `is_enum< future_errc >` will be `false_type`.
2014-06-05 00:19:04 +04:00
2019-04-23 02:00:26 +02:00
The emulated scoped enum can not be used in switch nor in template arguments. For these cases the user needs to use some helpers. Instead of
2014-06-05 00:19:04 +04:00
switch (ev)
{
case future_errc::broken_promise:
// ...
use
switch (boost::native_value(ev))
{
case future_errc::broken_promise:
// ...
2014-06-12 00:15:52 +04:00
and instead of
2014-06-05 00:19:04 +04:00
template <>
struct is_error_code_enum< future_errc > :
public true_type
{
};
use
template <>
struct is_error_code_enum< BOOST_SCOPED_ENUM_NATIVE(future_errc) > :
public true_type
{
};
2019-04-23 02:00:26 +02:00
Explicit conversion to the underlying type should be performed with `boost::underlying_cast` instead of `static_cast`:
2014-06-05 00:19:04 +04:00
unsigned int val = boost::underlying_cast< unsigned int >(ev);
2019-04-23 03:06:35 +03:00
In C++03, scoped enums behave differently in case of calling an overloaded function when one overload takes a scoped enum as a parameter, and the other takes a parameter of an integral type. Consider the following code:
2019-04-23 02:00:26 +02:00
enum enum_regular { REGULAR_A, REGULAR_B };
BOOST_SCOPED_ENUM_DECLARE_BEGIN(enum_scoped)
{
a, b
}
BOOST_SCOPED_ENUM_DECLARE_END(enum_scoped)
void regular_or_int(enum_regular); // (1)
void regular_or_int(int); // (2)
void scoped_or_int(enum_scoped); // (3)
void scoped_or_int(int); // (4)
regular_or_int(REGULAR_A); // calls (1) in C++03 and C++11
scoped_or_int(enum_scoped::a); // calls (3) in C++11 but (4) in C++03!
scoped_or_int(enum_scoped(enum_scoped::a)); // calls (3) in C++03 and C++11
2014-06-12 00:15:52 +04:00
Here is usage example:
2014-06-05 00:19:04 +04:00
2014-06-12 00:15:52 +04:00
BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(algae, char)
{
green,
red,
cyan
}
BOOST_SCOPED_ENUM_DECLARE_END(algae)
2014-06-05 00:19:04 +04:00
...
algae sample( algae::red );
void foo( algae color );
...
sample = algae::green;
foo( algae::cyan );
2014-06-05 10:48:21 -07:00
[endsect]
[section Deprecated syntax]
2014-06-05 00:19:04 +04:00
In early versions of the header there were two ways to declare scoped enums, with different pros and cons to each.
The other way used a different set of macros:
BOOST_SCOPED_ENUM_START(algae)
{
green,
red,
cyan
};
BOOST_SCOPED_ENUM_END
...
BOOST_SCOPED_ENUM(algae) sample( algae::red );
void foo( BOOST_SCOPED_ENUM(algae) color );
...
sample = algae::green;
foo( algae::cyan );
2014-06-06 12:32:43 +04:00
Here `BOOST_SCOPED_ENUM_START` corresponds to `BOOST_SCOPED_ENUM_DECLARE_BEGIN`, `BOOST_SCOPED_ENUM_END` to `BOOST_SCOPED_ENUM_DECLARE_END`
2014-06-05 00:19:04 +04:00
and `BOOST_SCOPED_ENUM` to `BOOST_SCOPED_ENUM_NATIVE`. Note also the semicolon before `BOOST_SCOPED_ENUM_END`.
In the current version these macros produce equivalent result to the ones described above and are considered deprecated.
2014-06-05 10:48:21 -07:00
[endsect]
[section Acquiring the underlying type of the enum]
2014-06-05 00:19:04 +04:00
The header `boost/core/underlying_type.hpp` defines the metafunction `boost::underlying_type` which can be used to
obtain the underlying type of the scoped enum. This metafunction has support for emulated scoped enums declared with
macros in `boost/core/scoped_enum.hpp`. When native scoped enums are supported by the compiler, this metafunction
is equivalent to `std::underlying_type`.
Unfortunately, there are configurations which implement scoped enums but not `std::underlying_type`. In this case
`boost::underlying_type` has to be specialized by user. The macro `BOOST_NO_UNDERLYING_TYPE` is defined to indicate
such cases.
2014-06-05 10:48:21 -07:00
[endsect]
2022-09-19 12:53:00 +03:00
[section Acknowledgements]
2014-06-05 00:19:04 +04:00
2014-06-05 22:47:32 +04:00
This scoped enum emulation was developed by Beman Dawes, Vicente J. Botet Escriba and Anthony Williams.
2014-06-05 00:19:04 +04:00
2014-06-05 22:47:32 +04:00
Helpful comments and suggestions were also made by Kjell Elster, Phil Endecott, Joel Falcou, Mathias Gaunard, Felipe Magno de Almeida,
Matt Calabrese, Daniel James and Andrey Semashev.
2014-06-05 00:19:04 +04:00
[endsect]
2014-06-05 10:48:21 -07:00
[endsect]