1

Take the following function:

template<typename T>
decltype(auto) find_median(T begin,
                           T end,
                           bool sorted = false,
                           auto comparison = [](auto a, auto b){return a < b;}){
    assert(begin != nullptr);
    assert(end != nullptr);
    return sorted ? find_median_sorted(begin, end) : find_median_unsorted(begin, end, comparison);
}

Note that I set the comparison param to a default value [](auto a, auto b){return a < b;}. So if I call this function like the following: find_median(std::addressof(arr), std::addressof(arr[9])) where arr is an std::array, this should work. But it doesn't work, does can someone tell me why?

1 Answer 1

6

You can provide a default value for a known type, but you can't provide a default value for a deduced type like this. It's just not something the language supports.

You have to provide a default for the type and the value:

template<typename T, typename Cmp = std::less<>>
decltype(auto) find_median(T begin,
                           T end,
                           bool sorted = false,
                           Cmp comparison = {})
{
    // ...
}
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.