tamas.kenez
tamas.kenez

Reputation: 7799

Can't pass std::min to function, copy of std::min works

Passing std::min to a function does not compile. I copied the libcpp declaration of std::min into my source file and it works.

What's wrong with the std version? Same happens with clang and gcc. Tested on godbolt: https://godbolt.org/g/zwRqUA

#include <thread>
#include <algorithm>

namespace mystd {
    // declaration copied verbatim from std::min (libcpp 4.0)
    template <class _Tp> inline constexpr const _Tp&
    mymin(const _Tp& __a, const _Tp& __b)
    {
        return std::min(__a, __b);
    }
}

int main()
{
    std::thread thr1(std::min<int>, 2, 3); // compile error
    std::thread thr2(mystd::mymin<int>, 2, 3); // works
    return 0;
}

The errors for clang and gcc:

[x86-64 clang 5.0.0 #1] error: no matching constructor for initialization of 'std::thread'

[x86-64 gcc 7.2 #1] error: no matching function for call to 'std::thread::thread(<unresolved overloaded function type>, int, int)'
[x86-64 gcc 7.2 #1] note:   couldn't deduce template parameter '_Callable'

Upvotes: 6

Views: 1388

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 310960

There are two template functions min overloaded for one template parameter. They are

template<class T> constexpr const T& min(const T& a, const T& b);

and

template<class T>
constexpr T min(initializer_list<T> t);

So the compiler does not know which one to select.

You could use an explicit casting of the function pointer to tell the compiler which function you mean.

Or you could use an intermediate pointer. For example

const int & ( *operation )( const int &, const int & ) = std::min<int>;

And then use the pointer operation instead of the function std::min.

Upvotes: 10

Ricardo Antunes
Ricardo Antunes

Reputation: 397

You can wrap std::min in a lambda, like this:

std::thread thr1([](int a, int b) { return std::min(a, b); }, 2, 3);

It doesn't work without the lambda wrapper because of the template argument ambiguity, just like @Vlad from Moscow explained.

Upvotes: 4

Related Questions