"Cannot convert" double (*) () noexcept 'to' double 'on initialization' when defining max double

I read that the standard way to use the C ++ maximum double value is std::numeric_limits<double>::max

.

Then, in each of my functions where I want to initialize my doubles as max double, I use:

#include <limits>
#define MAX_DOUBLE (std::numeric_limits<double>::max)

      

Using gcc -pedantic -pedantic-errors -Wall -Wextra -Werror I get the following error:

Cannot convert 'double (*)() noexcept' to 'double' in initialization

      

Can you explain this error?

+3


source to share


3 answers


std::numeric_limits<double>::max

is a function in the global namespace, not a constant. If you create a macro you need to specify the definition as a function identifier

#define MAX_DOUBLE std::numeric_limits<double>::max()

      



So passing MAX_DOUBLE in a double identifier, as the error message indicates, means initializing the double with a function pointer, which in this case is of type double (*)() noexcept

, causing a type mismatch.

Finally, for all the practical purposes DBL_MAX

defined in the climits , it had to serve your purpose, and thus you would not go through this agony,

0


source


As stated in the error message, you are directly using the function pointer ( double (*)() noexcept

) as double

. std::numeric_limits<double>::max

is declared as a function, you need to call it to get the value.

You can change

#define MAX_DOUBLE (std::numeric_limits<double>::max)

      



to

#define MAX_DOUBLE (std::numeric_limits<double>::max())

      

+3


source


You need to define it as:

#define MAX_DOUBLE std::numeric_limits<double>::max()

      

+2


source







All Articles