【发布时间】:2013-02-13 12:36:34
【问题描述】:
违规代码:
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()>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
据我所知,我应该能够在 c++11 中以这种方式使用numeric_limits。是这里的clang错了,还是我不知道什么?
编辑:
编译标志是:clang++ -o foo.o -c -W -Wall -Wextra -Werror -std=c++11 -stdlib=libc++ -g -I. foo.cpp
【问题讨论】:
-
您在 Clang 中使用哪个标准库?这不是编译器问题,这取决于您的标准库实现是否在
numeric_limits<T>::max()之前放置了constexpr标识符。此外,3.0 相当旧,因此如果您使用 libc++,它可能太旧而无法进行更改。 -
我猜自从添加
-stdlib=libc++之后没有任何变化,可能是clang太旧的问题? -
你是
using std::size_t还是using namespace std? -
是的,我在某个时候有命名空间。它不会改变任何东西。
-
将
size_t切换到unsigned long会产生同样的错误吗?