为什么VC ++编译代码而clang没有?

时间:2016-09-03 02:42:57

标签: c++ c++11 visual-c++ compiler-errors clang

我使用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):错误:没有可行的从''转换为'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;
}

根本原因是什么?

2 个答案:

答案 0 :(得分:6)

std::isspace在标准库中被重载。

由于标准库头的结构,一个编译器会看到两个不同的名称声明。

然后它的使用没有参数或投射是不明确的。

答案 1 :(得分:4)

std::isspace含糊不清,可以引用<cctype>function找到与{C}兼容的function template,也可以引用<locale>中找到的https://angular.io

您可以使用

解决歧义
std::function<int(int)> fn = static_cast<int(*)(int)>(std::isspace);

或者省略std::命名空间,虽然从技术上讲,没有要求将C函数导入全局命名空间的实现。

<codecvt>的Clang和GCC实现似乎都包含来自<locale>的模板声明,因此错误;大概是VS没有。

相关问题