简体   繁体   中英

How to define an enum template (C++20)?

enum ByteOrder :uint16_t
{
    LittleEndian = 0x4949,
    BigEndian = 0x4D4D,
};
template<ByteOrder BO>
enum SF :uint16_t;
template<>
enum SF<LittleEndian>
{
    SF_A = 0x0001;
};
template<>
enum SF<BigEndian>
{
    SF_A = 0x0100;
};

Code above is to define an enum template that can adapt itself between 2 different byte orders. However, the compiler says, "An enum template declaration must refer to a previously declared class template member." What does it mean?

Is it possible to define an enum template, but requires a special syntax configuration?

There are no enum templates in C++.

You can achieve the desired effect by making the enum a member of some class template.

template <ByteOrder> class SF_Wrapper;
template <> class SF_Wrapper<LittleEndian> { enum SF ...

template <ByteOrder bo> using SF = SF_Wrapper<bo>::SF;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM