使用variadic模板进行函数调用的模板类型扣除
Template type deduction with variadic template for function calls
我正在使用VC /std:c++latest
,我想确定成员函数的ret-type,类类型和参数类型作为模板参数传递给struct。我找到了这样做的方法:
template <auto MEMBER>
class C;
template <class RET, class T, class... ARGS, RET(T::*MEMBER)(ARGS...)>
class C<MEMBER>
{
public:
template <class... ARGS2>
RET operator()(ARGS2&&... args)
{
// place holder
(reinterpret_cast<T*>(0)->*MEMBER)(std::forward<ARGS2>(args)...);
}
};
struct A
{
void f(int, int) {}
};
int main()
{
C<&A::f> c; // error C2079: 'instance' uses undefined class 'C'
c(5, 5);
}
,但此解决方案仅与G 一起工作。
so
- 这是VC ? 中的错误
- 有其他方法可以实现同一件事吗?
不是答案(对不起),而是一个长评论:如果您想要完美的转发,则需要模板函数中的通用引用/方法
我的意思是...我建议重写operator()
如下或类似的内容(我还为T
对象添加了完美的转发)
template <typename U, typename ... As>
RET operator()(U && u, As && ... args)
{
(std::forward<U>(u).*MEMBER)(std::forward<As>(args)...);
}
,您可以写(至少在g 和clang 中)
A a;
C<&A::f> c;
c(a, 5, 5);
@max66的提示让我弄清楚,我不需要推断该函数的参数,可以轻松推导返回类型和类型。
template <auto MEMBER>
struct C
{
template <class RET, class T, class... ARGS>
static constexpr auto deduce_RET(RET (T::*member)(ARGS...)) -> RET;
template <class RET, class T, class... ARGS>
static constexpr auto deduce_T(RET (T::*member)(ARGS...)) -> T;
using RET = decltype(deduce_RET(MEMBER));
using T = decltype(deduce_T(MEMBER));
template <class... ARGS>
RET operator()(ARGS&&... args)
{
return (reinterpret_cast<T*>(0)->*MEMBER)(std::forward<ARGS>(args)...);
}
};
编辑:该错误已修复为Visual Studio 2019:https://develovelerCommunity.visualstudio.com/content/problem/464355/error-c2079-instance-uses-uses-use-uns-unsundelist-undecin--class-undefined-class-class-class-c.html/div>
相关文章:
- ArduinoJson 6.15.2:JsonObject没有命名类型
- 防止主数据类型C++的隐式转换
- 大量序列中核苷酸类型的快速计数
- 如何从C++中的依赖类型中获得它所依赖的类型
- std::绑定variadic模板和自动返回类型
- 类型检测:使用variadic参数正确实现计算平均值的函数
- 模板模板参数,具有混合类型和非类型variadic参数
- variadic函数模板的参数被作为模板非类型传递
- 采用数组类型的variadic函数
- 类型和标量常数的variadic模板
- 使用variadic模板进行函数调用的模板类型扣除
- 用类型的递归继承获得Variadic模板的阴影成员
- Variadic模板参数包只接受无符号int或size_t作为其类型
- 动态值的Variadic Template类型
- 如何将元函数应用于variadic模板类的模板类型
- 包括variadic模板中相同类型的数组
- variadic模板递归返回类型扣除汇编误差
- 如何使variadic模板类方法以函数指针为参数,类型派生自函数模板
- Variadic如何创建存储传递参数的类型
- getter 到 variadic 容器 - get<0>() 只处理具体类型?