2

I have a type defined using typedef unsigned int value_t; and a function

value_t find_minimal_value(...) {
    ...
    if(...) return numeric_limits<value_t>::max;
    ...
}

Compiler refuses to compile it, saying: invalid conversion from ‘int (*)()noexcept (true)’ to ‘value_t {aka int}’.

What does it mean? Looking into the numeric_limits class, the min() function should return a variable of the type passed to it via template typename, so value_t in this case. So why the code doesn't compile?

2
  • 4
    std::numeric_limits<T>::max() is a function. Commented Aug 26, 2014 at 22:34
  • 3
    @Nemo This is ridiculous, thanks. Commented Aug 26, 2014 at 22:35

1 Answer 1

8

std::numeric_limits::max() is a function, so you need to return the result of its invocation. That is done using the call operator:

value_t find_minimal_value() {
    if (...) return numeric_limits<value_t>::max();
    //                                          ^^
}

The error message meant that it couldn't convert a function pointer (i.e int (*)() to unsigned int.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.