【问题标题】:Deleted template function works on gcc, but not on clang [duplicate]删除的模板函数适用于 gcc,但不适用于 clang [重复]
【发布时间】:2017-07-24 15:29:40
【问题描述】:

我在使用已删除模板函数的模板特化时遇到问题。下面的代码显示了归结为 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>()

【问题讨论】:

标签: c++ gcc clang


【解决方案1】:

这看起来是一个错误。如果你用 clang 3.9.1 或更高版本编译它会编译。以下在 Golbolt 和 Wandbox 上使用 clang 3.8.1 的示例失败,但是当我们更改为 3.9.1 时,它们都可以编译。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-11
    • 2022-11-26
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    • 2014-11-26
    相关资源
    最近更新 更多