简体   繁体   中英

'Cannot convert 'double (*)() noexcept' to 'double' in initialization' when defining a max double

I read that the standard C++ way of using the maximum double value is std::numeric_limits<double>::max .

Then in each of my functions where I want to initialize my doubles as the 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?

Just as the error message said, you're using a function pointer ( double (*)() noexcept ) as a double directly. 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())

您需要将其定义为:

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

std::numeric_limits<double>::max is a function in the global namespace rather than a constant. If you are creating the macro, you need to specify the definition as a function identifier

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

So passing around MAX_DOUBLE to a double identifier as the error message indicates would mean, initializing a double with a function pointer, the type of which in this case is double (*)() noexcept which causes a type mismatch.

Finally for all practical purposes DBL_MAX defined in climits should have served your purpose and thus you would not have been through this torment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM