2017-07-24 22 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他們都編譯。

相關問題