[英]Why is the Visual Studio C++ Compiler rejecting an enum as a template parameter?
我正在使用Microsoft Visual Studio 2019編譯器(cl.exe),它拒絕了Clang和GCC接受的一些代碼,這些代碼與將枚舉用作模板參數有關,其中模板專門用於特定的枚舉值。
enum Foo {
Bar,
Baz
};
template<enum Foo = Bar> class Clazz {
};
template<> class Clazz<Baz> {
};
VC ++編譯器報告有關模板專業化的幾個錯誤:
<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)
Clang和GCC均接受此代碼,沒有錯誤。 這是VC ++的錯誤嗎?
用“ int”代替模板聲明中的“ enum Foo”會導致錯誤消失。 但是,這不是可接受的答案,因為我正在嘗試將大型代碼庫移植到VC ++。
如果您使用標准符合性模式編譯器選項/permissive-
來指定符合標准的編譯器行為,則將編譯您的代碼。
您可以在命令行或“項目屬性頁-> C / C ++->語言->一致性模式”中添加該選項。
您缺少電話:
template<enum Foo T = Bar> class Clazz {
或者有一個額外的枚舉:
template<Foo = Bar> class Clazz {
感謝François,第二個要好得多。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.