2013-02-13 96 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