我有一个简单的程序,可以使用MinGW的C / C ++库成功地使用clang进行编译:
#include <stdio.h>
int main(int argc, char **argv) { printf("Hello world!\n"); return 0; }
Run Code Online (Sandbox Code Playgroud)
我能够使用mingw-gcc成功编译此代码:
$ gcc test.c -o test
$ ./test
Hello world!
Run Code Online (Sandbox Code Playgroud)
我也可以使用clang + mingw成功编译它:
$ clang test.c -o test -target
$ ./test
Hello world!
Run Code Online (Sandbox Code Playgroud)
但是,如果我对程序(包括float.h)进行了少量更改,它将继续使用gcc进行编译,但不再使用clang进行编译:
#include <stdio.h>
#include <float.h>
int main(int argc, char **argv) { printf("Hello world!\n"); return 0; }
Run Code Online (Sandbox Code Playgroud)
$ gcc test.c -o test
$ ./test
Hello world!
$ clang test.c -o test -target x86_64-pc-windows-gnu
In file included from test.c:2:
In file included …Run Code Online (Sandbox Code Playgroud) 我正在使用Microsoft Visual Studio 2019编译器(cl.exe),它拒绝了Clang和GCC接受的一些与使用枚举作为模板参数有关的代码,其中模板专门用于特定的枚举值。
enum Foo {
Bar,
Baz
};
template<enum Foo = Bar> class Clazz {
};
template<> class Clazz<Baz> {
};
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)
Clang和GCC均接受此代码,没有错误。这是VC ++的错误吗?
用“ int”代替模板声明中的“ enum Foo”会导致错误消失。但是,这不是可接受的答案,因为我正在尝试将大型代码库移植到VC ++。