I'm using the Microsoft Visual Studio 2019 compiler (cl.exe), and it is rejecting some code accepted by both Clang and GCC, related to using enums as template parameters, where the templates are specialized for particular enum values.
enum Foo {
Bar,
Baz
};
template<enum Foo = Bar> class Clazz {
};
template<> class Clazz<Baz> {
};
The VC++ compiler reports several errors on the template specialization:
<source>(10): error C2440: 'specialization': cannot convert from 'Foo' to 'Foo'
<source>(10): note: Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast)
This code is accepted without errors by both Clang and GCC. Is this a bug with VC++?
Replacing the 'enum Foo' in the template declaration with just 'int' causes the errors to go away. However, this is not an acceptable answer, as I'm trying to port a large code base over to VC++.
Your code will compile if you use the Standards Conformance Mode compiler option /permissive-
to specify standards-conforming compiler behavior.
You can add that option on the command line or in the "Project property page -> C/C++ -> Language -> Conformance mode".
You are missing a T:
template<enum Foo T = Bar> class Clazz {
Or have an extra enum:
template<Foo = Bar> class Clazz {
This second one is much nicer, thanks François.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With