为 std::function 设置调用约定

Setting a calling convention for std::function

本文关键字:调用 约定 设置 function std      更新时间:2023-10-16

我对使用 std 很陌生,我目前正在尝试调用一个将 std::function 作为参数的函数。类似于以下内容:

在一个库的 .h 文件中:

typedef bool t (/*Params*/);
void __stdcall Foo(std::function<t> &function) {m_function = function;}
std::function<t> m_function;

我导入了库并尝试在另一个 cpp 文件中使用 Foo:

bool Implementation (/*Params*/)
{
   // Implementation
}
void Bar()
{
    Foo(std::function<t> (Implementation));
}
由于

调用约定,我在为 x86(但不是 x64)编译时收到链接器错误 (LNK2019):

Unresolved External Symbol __stdcall Foo (class std::tr1::function<bool __cdecl(/*Params*/) const&)

由此我了解到我需要将"t"和实现标记为__stdcall但这样做会导致其他编译失败。我还应该注意在同一个库中构建代码时正确编译的代码。有没有办法将调用约定与 std::function 相关联?

尝试:

void Foo(const std::function<bool()> &func)
{
  func();
}
bool Implementation (/*Params*/)
{
  cout << "Implementation"<<endl;
  return true;
}
void Bar()
{
    Foo(std::function<bool()>(&Implementation));
}
int main()
{
  Bar();
  return 0;
}