I misunderstood the question at first. If you are stuck pre C++20,
I've used this workaround in the past.
#include <iostream>
//-----------------------------------------------------------------------------
namespace foo
{
namespace bar
{
enum class Bar
{
A = 1,
B = 2,
C = 4,
D = 8
};
// define some constants in namespace bar with the same values as the
// enum values. this will maintain typesafety of function calls.
// but allows you to type using namespace foo::bar
// to access these values
constexpr Bar A = Bar::A;
constexpr Bar B = Bar::B;
constexpr Bar C = Bar::C;
constexpr Bar D = Bar::D;
}
}
//-----------------------------------------------------------------------------
using namespace foo::bar;
// function to show type of enum class can still be used as before
void function(const Bar enum_value)
{
std::cout << static_cast<int>(enum_value);
}
// function for oring Bar's
Bar operator|(const Bar a, const Bar b)
{
return static_cast<Bar>(static_cast<int>(a) | static_cast<int>(b));
}
// and a main to show it all
int main()
{
function(A|B|C|D); // outputs 15
return 0;
}