dinox0r
dinox0r

Reputation: 16049

Including <algorithm> and <limits> causes "invalid pure specifier" compilation error

Given this code:

#include <algorithm>
#include <limits>

int main() {
    const char INFINITY = std::numeric_limits<char>::max();

    return 0;
}

If I compile it with g++ I get the following error:

$ g++ --std=c++11 test.cpp

test.cpp: In function ‘int main()’:
test.cpp:5:30: error: invalid pure specifier (only ‘= 0’ is allowed) before ‘::’ token
     const char INFINITY = std::numeric_limits<char>::max();
                              ^
test.cpp:5:58: error: function ‘const char __builtin_inff()’ is initialized like a variable
     const char INFINITY = std::numeric_limits<char>::max();

Removing the #include <algorithm> statement fixes the problem.

My question: Is this a bug or is this the expected behaviour?

If this is the expected behaviour, is there some namespace clashing rule that I'm not aware of?

g++ version:

gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.1)

Upvotes: 0

Views: 403

Answers (1)

Mats Petersson
Mats Petersson

Reputation: 129494

INFINITY is a macro defined by the C and C++ standards in <limits> (or C's limits.h) - it represents the value of infinity for a double (e.g. 0x7ff0000000000000 for IEEE-754). Quite often, this is defined as a "builtin function" that the compiler replaces with a constant during later stages of the code generation. Hence the rather strange error message. And since Macros just expand in situ with no namespace etc, it's not possible to use the name INFINITY for anything else.

Upvotes: 4

Related Questions