简体   繁体   中英

C++: How to define an enum class outside of a template class body in which it is declared?

I have some C++ code that takes the following form:

template <typename type>
class foo
{
    type a;
    class bar;
};

template <typename type>
class foo<type>::bar
{
    enum class baz;
};

template <typename type>
enum class foo<type>::bar::baz
{
    val1,
    val2
};

With this code, I'm trying to get the enum class to be accessible by methods inside foo::bar and to be able to store data of the type of this enum class. The enum class is also not meant to be of a template type - the enum class enumerators are integers/the default type of an enum class.

However, when I compile this is MinGW/Code::Blocks this seems to produce two error message, both on the line:

enum class foo<type>::bar::baz

error: template declaration of 'enum baz'

error: foo<type>::bar has not been declared

I think this is almost certainly a compiler bug. Based on temp.mem.class and temp.mem.enum , I'd say this should definitely be valid C++. clang as well as icc seem to compile this code just fine. GCC (MinGW is basically GCC) as well as MSVC apparently fail to compile this, however. It seems both compilers (even in their most recent versions) mistake this definition of an enum member of a class template for an attempt to declare an enum template (which would indeed be illegal)…

quick test here

Edit: In case of MSVC, there seems to already be an open issue here

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