2013-02-13 19 views
2

違規代碼:C++ 11:clang在我的模板定義中拒絕numeric_limits <>,而gcc接受它 - 這是正確的?

template <class Bar, 
     size_t MAX_SIZE = std::numeric_limits<size_t>::max()> 
size_t foo(Bar const& b) { omitted... } 

它編譯於GCC 4.7.2與-std=c++11罰款。在鐺3.0我得到以下錯誤:

foo.hpp:35:28: error: non-type template argument of type 'unsigned long' is not an integral constant expression 
     size_t MAX_SIZE = std::numeric_limits<size_t>::max()> 
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

至於我可以告訴大家,我應該能夠以這種方式在C++ 11使用numeric_limits。鏗鏘在這裏是錯誤的,還是我不知道的東西?

編輯:

編譯標誌是:clang++ -o foo.o -c -W -Wall -Wextra -Werror -std=c++11 -stdlib=libc++ -g -I. foo.cpp

+2

你鏗鏘使用哪種STDLIB?這不是一個編譯器問題,它取決於標準庫實現是否在'numeric_limits :: max()'之前放置'constexpr'標識符。另外,3.0還是比較老的,所以如果你使用libC++,那麼可能只是太老而不能做出改變。 – Xeo 2013-02-13 12:38:20

+0

我猜想因爲添加'-stdlib = libC++'沒有改變,它可能是一個太老的問題? – porgarmingduod 2013-02-13 12:53:59

+0

你在使用std :: size_t還是使用namespace std? – 2013-02-13 12:58:40

回答

2

您的代碼在clang ++ 3.2中編譯得很好,請參閱here

我會說你的代碼沒有問題,但你應該升級到更新版本的鏗鏘聲。

注:由於編譯器的bug(感謝@Xeo)的代碼不與英特爾C++編譯器編譯13.0.1:

Compilation finished with errors: 
source.cpp(6): internal error: assertion failed: ensure_il_scope_exists: NULL IL scope (shared/cfe/edgcpfe/il.c, line 7439) 

size_t MAX_SIZE = std::numeric_limits<size_t>::max()> 
^ 

compilation aborted for source.cpp (code 4) 
+0

「內部錯誤」應該告訴你這是一個編譯器錯誤。 – Xeo 2013-02-13 13:07:43

+0

Ooops,謝謝,我錯過了...... – Ali 2013-02-13 13:08:13

+0

maxng()'compiles,但'numeric_limits (max)()'不會。它雖然用gcc編譯。 [nlohmann/json](https://github.com/nlohmann/json)包有這個問題。 – user9645 2017-10-18 19:42:56

相關問題