【问题标题】:Why does VC++ compile the code while clang doesn't?为什么 VC++ 编译代码而 clang 不编译?
【发布时间】:2017-01-11 04:22:00
【问题描述】:

我使用 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;

更奇怪的是,如果我把第一行的cets如下,clang也可以。

//#include <codecvt> // now clang feels happy
#include <cctype>
#include <functional>

int main()
{
    std::function<int(int)> fn = std::isspace;
}

根本原因是什么?

【问题讨论】:

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


    【解决方案1】:

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

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

    那么它在没有参数或强制转换的情况下使用是模棱两可的。

    【讨论】:

      【解决方案2】:

      std::isspace 是模棱两可的,它可以引用 &lt;cctype&gt; 中的 function 以与 C 兼容,或者引用 &lt;locale&gt; 中的 function template

      你可以用

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

      或者通过省略 std:: 命名空间,尽管从技术上讲,实现不需要将 C 函数导入全局命名空间。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-14
        • 2011-05-16
        • 2013-02-20
        相关资源
        最近更新 更多