"reinterpret_cast":无法通过速推从'overloaded-function'转换为'intptr_t'.dll
'reinterpret_cast': cannot convert from 'overloaded-function' to 'intptr_t' with boost.dll
我正在使用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>
:(
相关文章:
- 表观调用前面的表达式必须具有指向 func 类型的指针
- 模板规范获取'Ambiguous call to overloaded function'
- C++ "error: use of overloaded operator '*' is ambiguous"似乎只有一场比赛
- 如何修复C++中的" No viable overloaded '=' "
- 不能在 if 语句 - c++ 中使用 void func 直接将字符串转换为大写
- 尝试查找两组点的交集,错误"No viable overloaded '=' "
- "reinterpret_cast":无法通过速推从'overloaded-function'转换为'intptr_t'.dll
- 在扣除自动之前使用decltype(auto) <func>
- Understanding void func(A())
- CPython 'overloaded'函数
- 为什么"std::isdigit"在 Windows 上报告C++"no matching overloaded function"
- DLL+导出类+模板成员func=未解析的外部符号.任何修复的机会
- 如何在派生类中不显式调用base::func()的情况下从基类执行虚拟函数
- C++语法错误,编译器不会警告或 int v = func(&v) 出错;
- (C++)构造函数、默认参数"call of overloaded... ambigous"
- cin.get() 导致"no instance of overloaded function"错误
- 为什么 decltype(declval()<T>.func()) 可以工作,而 decltype(&T::func) 不能工作?
- 错误 C2679:二进制'<<':找不到采用类型 'overloaded-function' 的右侧操作数的运算符(或者没有可接受的转换)
- 是否可以交换在 c++ 中作为函数 func(say) 中的参数传递的 const 向量的两个元素,例如:vector<int>func (const vector<int>&
- C++,如何将返回值的函数传递给 func