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

情到浓时终转凉″ 提交于 2021-02-08 15:48:19

问题


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'

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/47109317/cant-pass-stdmin-to-function-copy-of-stdmin-works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!