Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在线程中使用stl算法_C++_Multithreading - Fatal编程技术网

C++ 在线程中使用stl算法

C++ 在线程中使用stl算法,c++,multithreading,C++,Multithreading,为什么我不能在这样的线程中使用STL算法: #include <iostream> #include <thread> #include <algorithm> int main() { std::thread t(std::max, 5, 6); t.join(); return 0; } 但是我得到了这个错误: error: no matching function for call to ‘std::thread::thr

为什么我不能在这样的线程中使用STL算法:

#include <iostream>
#include <thread>
#include <algorithm>

int main() {

    std::thread t(std::max, 5, 6);
    t.join();

    return 0;
}
但是我得到了这个错误:

error: no matching function for call to ‘std::thread::thread(<unresolved overloaded function type>, int, int)’
     std::thread t(std::max<int>, 5, 6);
note: candidate: template<class _Callable, class ... _Args> std::thread::thread(_Callable&&, _Args&& ...)
       thread(_Callable&& __f, _Args&&... __args)
note:   template argument deduction/substitution failed:
note:   couldn't deduce template parameter ‘_Callable’
     std::thread t(std::max<int>, 5, 6);
note: candidate: std::thread::thread(std::thread&&)
     thread(thread&& __t) noexcept
note:   candidate expects 1 argument, 3 provided
note: candidate: std::thread::thread()
thread() noexcept = default;
note:   candidate expects 0 arguments, 3 provided
有,这就是为什么你会犯错误。使用std::max并不能消除歧义,因为仍然存在与std::max匹配的重载:


我强烈建议使用lambda函数,因为您可以准确地表达您期望函数的行为方式。此外,lambda可以很好地使用默认参数。

第一个参数可能是最大值的不确定性。第二个参数除了缺少分号外看起来还可以。@user4581301抱歉,第二个参数确实有效。如何修复第一个示例?我尝试:线程tmax,5,6,但是它不起作用。max仍然不够具体。你需要像int-const&*int-const&,int-const&std::max这样的东西来澄清你指的是哪一个max。@marglisse,thx,它是有效的。@Marc狗娘养的。一小时前,我用一个using语句戳了戳它,但我无法让它工作,因为我在返回类型中遗漏了const。
#include <iostream>
#include <thread>
#include <algorithm>

int main() {

    std::thread t(std::max<int>, 5, 6);
    t.join();

    return 0;
}
error: no matching function for call to ‘std::thread::thread(<unresolved overloaded function type>, int, int)’
     std::thread t(std::max<int>, 5, 6);
note: candidate: template<class _Callable, class ... _Args> std::thread::thread(_Callable&&, _Args&& ...)
       thread(_Callable&& __f, _Args&&... __args)
note:   template argument deduction/substitution failed:
note:   couldn't deduce template parameter ‘_Callable’
     std::thread t(std::max<int>, 5, 6);
note: candidate: std::thread::thread(std::thread&&)
     thread(thread&& __t) noexcept
note:   candidate expects 1 argument, 3 provided
note: candidate: std::thread::thread()
thread() noexcept = default;
note:   candidate expects 0 arguments, 3 provided
template< class T >
constexpr const T& max( const T& a, const T& b );

template< class T >
constexpr T max( std::initializer_list<T> ilist );
std::thread t(static_cast<const int&(*)(const int&, const int&)>(std::max), 5, 6);
std::thread t([](int a, int b) { return std::max(a, b); }, 5, 6);