porgarmingduod
porgarmingduod

Reputation: 7888

c++11: clang refuses numeric_limits<> in my template definition, while gcc accepts it - which is right?

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

Upvotes: 2

Views: 1628

Answers (2)

Ali
Ali

Reputation: 58501

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)

Upvotes: 2

Jonathan Wakely
Jonathan Wakely

Reputation: 171373

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.

Upvotes: 1

Related Questions