有问题的代码:
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
。在 clang 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()>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
据我所知,我应该能够使用 numeric_limits
在c++11中就是这样。是 clang 错了,还是我不知道什么?
编辑:
编译标志为:clang++ -o foo.o -c -W -Wall -Wextra -Werror -std=c++11 -stdlib=libc++ -g -I. foo.cpp
最佳答案
您的代码可以在 clang++ 3.2 中正常编译,请参阅 here .
我想说你的代码没有任何问题,但你应该升级到更新版本的 clang。
注意:由于编译器错误,该代码无法使用英特尔 C++ 编译器 13.0.1 进行编译(感谢 @Xeo):
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)
关于c++11:clang 在我的模板定义中拒绝 numeric_limits<>,而 gcc 接受它 - 这是正确的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14853731/