我正在使用Boost开发插件系统.dll
#include <boost/config.hpp>
#include <boost/dll/alias.hpp>
#include <memory>
class base {
public:
base(){};
~base(){};
template <typename T>
static std::shared_ptr<base> create() {
return std::make_shared<T>();
}
virtual void do1() = 0;
};
class derived : public base {
public:
derived(){};
~derived(){};
virtual void do1() override {}
};
BOOST_DLL_ALIAS(base::create<derived>, // <-- this function is exported with...
create_plugin // <-- ...this alias name
)
// auto a = base::create<derived>();
当我尝试在宏中使用工厂方法BOOST_DLL_ALIAS出现如下错误时。
cl /c /ID:DownloadCompressedboost_1_67_0 a.cpp Microsoft (R) C/C++ Optimizing Compiler Version 19.12.25834 for x86 Copyright (C) Microsoft Corporation. All rights reserved. a.cpp a.cpp(27): error C2440: 'reinterpret_cast': cannot convert from 'overloaded-function' to 'intptr_t' a.cpp(27): note: Context does not allow for disambiguation of overloaded function
调用工厂方法时没有BOOST_DLL_ALIAS宏(如最后注释行所示(,它可以编译正常。
// this really does nothing:
template<class R, class...Args>
R(*to_fptr( R(*f)(Args...) ))(Args...) {
return f;
}
// except avoid the bug in MSVC:
BOOST_DLL_ALIAS(to_fptr(base::create<derived>),
create_plugin
)
那应该可以解决它。
当您尝试从模板函数的名称转换为intptr_t时,MSVC 似乎中断。 这是一个错误。
上述解决方法将其从直接处理模板函数的名称更改为仅处理函数指针。 通过打破过载分辨率和演员intptr_t分开,MSVC 不再阻塞。
您可能还可以执行以下操作:
template<class T>
T identity( T t ) { return std::move(t); }
而不是to_fptr
.
to_fptr
是一个接受函数指针并返回它的函数。 从函数返回函数指针的语法有点荒谬,这就是为什么它很难阅读的原因。
受到这个切线相关答案的启发,我找到了另一个解决方案:
const void * create_plugin =
reinterpret_cast<const void*>(
reinterpret_cast<std::intptr_t>(
reinterpret_cast<std::decay_t<decltype(&base::create<derived>)> >(
&base::create<derived>
)
)
);
https://godbolt.org/z/NVGz_0
这应该可以解决问题:
BOOST_DLL_ALIAS(std::function<std::shared_ptr<base>(void)>(base::create<derived>), // <-- this function is exported with...
create_plugin // <-- ...this alias name
)
事实上,MSVC 似乎在这里有一个错误,因为将这样的表达式base::create<derived>
并直接将其视为函数指针应该没有问题。因此,相反,我们使用 std::function 的魔力自己"给它一个函数指针"。
我选择使用std::function
,因为它不涉及编写任何类型的包装代码,并且直接依赖于意图交付的标准 c++ 代码。
当然不要忘记#include <functional>
:(