(boost:: && std::) 绑定仅在参数太少的 MSVC 上失败

(boost:: && std::) bind fails only on MSVC with too few arguments

本文关键字:失败 参数 MSVC std boost 绑定      更新时间:2023-10-16

编辑:最小化代码示例

#include <iostream>
#include <functional>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <Wt/WServer>
Wt::WApplication *createApplication(const Wt::WEnvironment& env, int i) {
    return new Wt::WApplication(env);
}
int main(int argc, char** argv)
{
    Wt::WRun(argc, argv,boost::bind(&createApplication, _1, 1));
}

错误94错误C2198: 'Wt::WApplication *(__cdecl *)(const Wt::WEnvironment &,int)':调用 Wt -3.3.4-msvs2013-windows-x86-sdkincludeboostfunctionfunction_template.hpp 95 1

Wt::WRun(argc, argv,std::bind(&createApplication, std::placeholders::_1, 1));中也会失败,并出现完全相同的错误。


老例子

我正在使用库Wt,它具有函数Wt::WRun(),作为函数的第三个参数,在这种情况下,application_creator,返回一个指向Wt类型的指针,并接受一个参数。到目前为止一切顺利。这个函数是用户提供的,可能需要更多的参数,我这样做了,并且还提供了一个库示例(参见main.c, Wt::WSever::addEntryPoint接受与WRun相同的参数)。

所以我想绑定我的附加参数,就像在例子中一样。我的解决方案与gcc/mingw完美编译,但与MSVC/Visual Studio 2013 Express,它失败的错误

错误94错误C2198: 'Wt::WApplication *(__cdecl *)(const Wt::WEnvironment &,int)':调用参数太少…包括boostfunctionfunction_template.h

My call: Wt::WRun(argc, argv,boost::bind(MDDB_Service::application_creator, _1, 5));

回调函数Wt::WApplication* MDDB_Service::application_creator(const Wt::WEnvironment& env, int foo);的定义

WT::WRun定义:

#define WTCONNECTOR_API __declspec(dllimport)
typedef boost::function<WApplication* (const WEnvironment&)> ApplicationCreator;
int WTCONNECTOR_API WRun(int argc, char** argv,
            ApplicationCreator createApplication = 0);

同理,Wt::WRun(argc, argv,std::bind(MDDB_Service::application_creator, std::placeholders::_1, 5));

错误94错误C2198: 'Wt::WApplication *(__cdecl *)(const Wt::WEnvironment &,int)':调用参数太少…包括促进函数 function_template.hpp

问题实际上不是编译器的错,而是这一行:

Wt::WEnvironment we();  

声明了一个名为we的函数,不带参数,返回一个Wt::WEnvironment。你正在成为最令人烦恼的解析的受害者。

替换行
Wt::WEnvironment we;  

应该可以解决您的问题。