2

The offending code:

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

It compiles fine on gcc 4.7.2 with -std=c++11. On clang 3.0 I get the following error:

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()>
                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

As far as I can tell, I am supposed to be able to use numeric_limits in this way in c++11. Is clang wrong here, or am I unaware of something?

EDIT:

Compilation flags are: clang++ -o foo.o -c -W -Wall -Wextra -Werror -std=c++11 -stdlib=libc++ -g -I. foo.cpp

ildjarn
  • 62,044
  • 9
  • 127
  • 211
porgarmingduod
  • 7,668
  • 10
  • 50
  • 83
  • 2
    Which stdlib do you use with Clang? This is not a compiler problem, it depends on whether your standard library implementation put a `constexpr` identifier before `numeric_limits::max()`. Also, 3.0 is rather old, so if you're using libc++ with that, it might just be too old to have the change. – Xeo Feb 13 '13 at 12:38
  • I guess since adding `-stdlib=libc++` changed nothing, it might be a problem of too old clang? – porgarmingduod Feb 13 '13 at 12:53
  • Are you `using std::size_t` or `using namespace std`? – André Puel Feb 13 '13 at 12:58
  • Yeah, I had the namespace in there at some point. It does not change anything. – porgarmingduod Feb 13 '13 at 13:04
  • Switching `size_t` to `unsigned long` yields the same error? – André Puel Feb 13 '13 at 13:22
  • Even `int` is the same. Look at the other answers, the problem is probably with the version of clang. – porgarmingduod Feb 13 '13 at 13:27

2 Answers2

2

Your code compiles just fine with clang++ 3.2, see here.

I would say there is nothing wrong with your code but you should upgrade to a newer version of clang.

Note: The code doesn't compile with the Intel C++ Compiler 13.0.1 due to a compiler bug (thanks @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)
Ali
  • 56,466
  • 29
  • 168
  • 265
  • Another clang bug (in 4.0) `numeric_limtsmax()` compiles, but `numeric_limits(max)()` does not. It does compile with gcc though. The [nlohmann/json](https://github.com/nlohmann/json) package has this issue. – user9645 Oct 18 '17 at 19:42
  • @user9645 That's disappointing... :( Clang 5.0 came out on Sep 7 this year. I wonder whether this issue is resolved in 5.0. – Ali Oct 18 '17 at 21:23
1

To use C++11 library features with clang you need to use the libc++ standard library implementation, otherwise you get the ancient library from GCC 4.1.2, which doesn't support C++11

See https://stackoverflow.com/a/14790442/981959 and https://stackoverflow.com/a/14150421/981959 and many other questions.

Community
  • 1
  • 1
Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521