在与数学相关的上下文中,我想定义函数的函子<cmath>.出于这个问题的目的,我们将使用std::invoke作为我们的函子.
这是不正确的(现场演示):
std::invoke(std::sin, 0.0);
Run Code Online (Sandbox Code Playgroud)
(g ++ - 8.1)错误:没有用于调用'invoke(<unresolved overloaded function type>,double)'的匹配函数
实际上,std::sin是一个重载集,编译器缺少类型信息来选择其中一个函数.
如何从重载集中命名特定函数?我们可以取代什么LEFT,RIGHT以便以下是良好的形式,并做出预期(例如,选择double std::sin(double))?
#include <functional>
#include <cmath>
int main()
{
(void) std::invoke(LEFT std::sin RIGHT, 0.0);
}
Run Code Online (Sandbox Code Playgroud)
如果这是不可能的,有没有办法定义一个仿函数,所以它是重载集感知?
void hello()
{
cout << "helloworld" << endl;
}
void hello(string s)
{
cout << "hello " << s << endl;
}
void doWork()
{
thread t1(static_cast<void ()>(&hello));
thread t2(static_cast<void (string)>(&hello),"bala");
t1.join();
t2.join();
}
Run Code Online (Sandbox Code Playgroud)
错误:
thread.cc|19 col 42| error: invalid static_cast from type '<unresolved overloaded function type>' to type 'void()'
thread.cc|20 col 48| error: invalid static_cast from type '<unresolved overloaded function type>' to type 'void(std::string) {aka void(std::basic_string<char>)}'
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用typedef函数指针或lambda.是不是可以使用static_cast?