2017-07-24 21 views
2

我遇到了模板特化的已删除模板函数的问题。下面的代码显示归结为MWE问题:删除的模板函数在gcc上工作,但不在叮当中

#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>() 
+1

也许Apple LLVM 8.0.0是针对[旧版本的C++ 11标准](https://stackoverflow.com/a/33258249/501250)编写的,它不允许对已删除函数进行专门化。是否有可用于此编译器的更新版本? – cdhowie

回答

1

这看起来是一个错误。如果你使用clang 3.9.1或更高版本进行编译,它将被编译。下面的例子在GolboltWandbox与叮当3.8.1失败,但当我们改变为3.9.1他们都编译。

相关问题