Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error C2589 on std::numeric_limits<double>::min()

When I try to compile some code (not my own) i get a C2589 '(':illegal token on right side of'::'

on this line:

    maxPosition[0]=std::numeric_limits<double>::min();

i guess this is because there is already a min() macro defined, but why is the compiler not taking the min() from the specified namespace instead of the macro?

like image 226
Mat Avatar asked Dec 01 '09 12:12

Mat


People also ask

What is Numeric_limits int min ()?

numeric_limits::minReturns the minimum finite value representable by the numeric type T . For floating-point types with denormalization, min returns the minimum positive normalized value. Note that this behavior may be unexpected, especially when compared to the behavior of min for integral types.

What does Numeric_limits do in C++?

The std::numeric_limits ::digits function is used to find the number of radix digits that the data type can represent without loss of precision.

What is Numeric_limits int >:: max ()?

std::numeric_limits::max(): The std::numeric_limits<T>::max() function is used to get the maximum finite value representable by the numeric type T. All arithmetic types are valid for type T. Header File: #include<limits>

Which of the following data types is accepted by the Numeric_limits function?

Data types that supports std::numeric_limits() in C++ std::numeric_limits<int>::max() gives the maximum possible value we can store in type int. std::numeric_limits<unsigned int>::max()) gives the maximum possible value we can store in type unsigned int.


2 Answers

but why is the compiler not taking the min() from the specified namespace instead of the macro?

Because macros don't care about your namespaces, language semantics, or your compiler. The preprocessing happens first.

In other words, the compiler only sees what is left after the preprocessing stage. And min was replaced by some replacement string, and the result is what the compiler saw.

like image 124
Alex Budovski Avatar answered Oct 07 '22 11:10

Alex Budovski


Hitting F12 on offending std::numeric_limits::min() function

Leads to some where like :

c:\Program Files (x86)\Windows Kits\8.1\Include\shared\minwindef.h

Where you will find:

#ifndef NOMINMAX

#ifndef max
#define max(a,b)            (((a) > (b)) ? (a) : (b))
#endif

#ifndef min
#define min(a,b)            (((a) < (b)) ? (a) : (b))
#endif

So adding

#define NOMINMAX

to top of your .cpp file (as the WINAPI does: see Windows.h as example) before any #include headers should rectify the problem.

like image 27
bitminer Avatar answered Oct 07 '22 11:10

bitminer