为什么 VC++ 编译代码而 clang 不编译?
Why does VC++ compile the code while clang doesn't?
我使用VS 2015 (Update 3)编译以下代码:
#include <codecvt>
#include <cctype>
#include <functional>
int main()
{
std::function<int(int)> fn = std::isspace;
}
如果我用vc++编译它,它是可以的。但是,如果我在Visual Studio中将编译器更改为Visual Studio 2015 - Clang with Microsoft CodeGen (v140_clang_c2)
, clang报告一个错误:
main.cpp(7,26): error: no viable conversion from 'std::function'
std::function fn = std::isspace;
更令人惊讶的是,如果我像下面这样注释第一行,clang也可以。
//#include <codecvt> // now clang feels happy
#include <cctype>
#include <functional>
int main()
{
std::function<int(int)> fn = std::isspace;
}
根本原因是什么?
std::isspace
在标准库中被重载。
由于标准库头文件的结构不同,一个编译器会看到两个不同的名称声明。
那么不带参数或强制转换使用它是有歧义的。
std::isspace
是不明确的,它既可以指<cctype>
中为与C兼容而找到的函数,也可以指<locale>
中找到的函数模板。
可以用
解决歧义std::function<int(int)> fn = static_cast<int(*)(int)>(std::isspace);
或者省略std::
命名空间,尽管技术上没有要求实现将C函数导入全局命名空间。
<codecvt>
的Clang和GCC实现似乎都包含了<locale>
的模板声明,因此出现了错误;
相关文章:
- 当我编译webrtc服务器时,Windows上只支持clang-cl
- 我可以将一个用clang c++11编译的对象与另一个用c++17编译的对象链接起来吗
- clang 插件:在编译过程中修改 AST
- 错误:使用 clang 没有可行的重载,使用 GCC 编译
- 在Linux上使用Clang / OLLVM交叉编译helloworld Windows可执行文件时的问题
- 通过自制软件(macOS)安装的clang++:编译错误
- 在使用 Clang 编译 DLL 时指定 DEF 文件
- 获取 clang++:错误:在编译文件系统库的代码时
- 如何在 clang 的自动会议中检查支持编译标志
- constexpr 使用 clang 编译 TensorFlow 时出错
- 编译包含指向模板函数的指针的初始值设定项列表时,gcc 出错,但 clang 不出错
- 如何更改 clang 的预编译头文件的路径
- 除了说明符神秘地破坏编译(Clang,GCC不同意)
- VS 代码显示编译 clang 的错误消息
- 同一段代码,可以用 xcode 或使用 g++ 编译,在终端中编译 clang++,不能使用 gcc 或 clang 编
- 简单的 constexpr 函数无法使用 GCC 编译(clang 没问题)
- 在 C++14 模式下为 libstdc++ 编译 clang 正则表达式程序会导致错误
- 如何在Windows上编译Clang
- 在Windows中编译Clang插件
- 是否可以在内存中使用Clang c++ API编译Clang源代码?