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'
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With