c++ - 删除的模板函数适用于 gcc,但不适用于 clang

标签 c++ gcc clang

<分区>

我在对已删除的模板函数进行模板特化时遇到问题。下面的代码显示了归结为 MWE 的问题:

#include <iostream>
#include <string>

template<typename T>
inline std::string typeToString() = delete;

template<>
inline std::string typeToString<float>()
{
    return "float";
}

int main()
{
    std::cout << typeToString<float>() << std::endl;
}

使用 gcc 7 可以正常编译。但是,使用 Apple LLVM 8.0.0 我收到以下错误消息:

clang test.cpp -std=c++1z
test.cpp:8:28: error: inline declaration of 'typeToString<float>' follows non-inline definition
    inline std::string typeToString<float>()
                       ^
test.cpp:8:28: note: previous definition is here
test.cpp:15:18: error: call to deleted function 'typeToString'
std::cout << typeToString<float>() << std::endl;
             ^~~~~~~~~~~~~~~~~~~
test.cpp:8:28: note: candidate function [with T = float] has been explicitly deleted
    inline std::string typeToString<float>()

最佳答案

这看起来是一个错误。如果您使用 clang 3.9.1 或更高版本编译,它将编译。下面的例子在GolboltWandbox clang 3.8.1 失败,但当我们更改为 3.9.1 时,它们都可以编译。

关于c++ - 删除的模板函数适用于 gcc,但不适用于 clang,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45284548/

相关文章:

c++ - 如果 QMenu 是 unique_ptr,为什么 QAction 不添加到 QMenu?

c++ - 删除NULL但没有编译错误

c++ - SFML - 创建光标和打印输入?

c++ - _mm_max_ss 在 clang 和 gcc 之间有不同的行为

c++ - SFINAE 在创建 "is_iterable"特征时出现问题 - 这是 gcc 错误吗?

c++ - VS2013 上的模板特化和 SFINAE

c++ - 类,错误 : ISO C++ forbids declaration of 'Task' with no type

c - 从 fgets() 输入中删除换行符

c - GCC 返回错误 - 'implicit declaration of function ' 截断''

clang - 在 clang-tidy 中,如何设置接受数字列表的检查选项