【问题标题】:c++11: clang refuses numeric_limits<> in my template definition, while gcc accepts it - which is right?c++11:clang 在我的模板定义中拒绝 numeric_limits<>,而 gcc 接受它——这是对的吗?
【发布时间】: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&lt;T&gt;::max() 之前放置了constexpr 标识符。此外,3.0 相当旧,因此如果您使用 libc++,它可能太旧而无法进行更改。
  • 我猜自从添加-stdlib=libc++之后没有任何变化,可能是clang太旧的问题?
  • 你是using std::size_t 还是using namespace std
  • 是的,我在某个时候有命名空间。它不会改变任何东西。
  • size_t 切换到unsigned long 会产生同样的错误吗?

标签: c++ gcc c++11 clang


【解决方案1】:

您的代码使用 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)

【讨论】:

  • 另一个 clang 错误(在 4.0 中)numeric_limts&lt;type&gt;max() 编译,但 numeric_limits&lt;type&gt;(max)() 没有。不过它确实用 gcc 编译。 nlohmann/json 包有这个问题。
  • @user9645 太令人失望了... :( Clang 5.0 于今年 9 月 7 日发布。我想知道这个问题是否在 5.0 中得到解决。
【解决方案2】:

要在 clang 中使用 C++11 库功能,您需要使用 libc++ 标准库实现,否则您会从不支持 C++11 的 GCC 4.1.2 获得古老的库

请参阅https://stackoverflow.com/a/14790442/981959https://stackoverflow.com/a/14150421/981959 以及许多其他问题。

【讨论】:

  • 使用clang++ -o foo.o -c -W -Wall -Wextra -Werror -std=c++11 -stdlib=libc++ -g -I. foo.cpp 编译没有任何改变。
猜你喜欢
  • 2019-01-31
  • 2015-07-03
  • 2016-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-11
  • 2014-07-08
相关资源
最近更新 更多