[英]C++ Variadic Template to Evaluate Pointer to Member
I would like to create a variadic template that evaluates nested pointer to members. 我想创建一个可变参数模板,用于评估指向成员的嵌套指针。 I have tried the following:
我尝试了以下方法:
template<typename T, typename U, typename... V>
auto getField(T &input, U (T::*field), V... args)
-> decltype(getField(input.*field, &args...))
{
getField(input.*field, &args...);
}
template<typename T, typename U>
U getField(T &input, U (T::*field))
{
return input.*field;
}
struct inner {
int val;
};
struct outer {
inner in;
};
void main() {
outer p{{5}};
cout << getField(p, &outer::in, &inner::val) << endl;
}
When I compile the above in VS, I get the following error messages: 当我在VS中编译以上代码时,出现以下错误消息:
error C2672: 'getField': no matching overloaded function found
错误C2672:“ getField”:找不到匹配的重载函数
error C2893: Failed to specialize function template 'unknown-type getField(T &, UT::* ,V...)'错误C2893:无法专用于功能模板'unknown-type getField(T&,UT :: *,V ...)'
note: With the following template arguments:注意:具有以下模板参数:
note: 'T=outer'注意:“ T =外部”
note: 'U=outer::inner'注意:“ U = outer :: inner”
note: 'V={int outer::inner::* }'注意:“ V = {int external :: inner :: *}”
How can I fix the above variadic template to compile, and return p.in.val
? 如何修复上述可变参数模板进行编译,然后返回
p.in.val
? Note that my compiler doesn't support auto template parameters. 请注意,我的编译器不支持自动模板参数。
Change order of methods and fix "typos": 更改方法的顺序并修复“ typos”:
template<typename T, typename U>
U getField(T &input, U (T::*field))
{
return input.*field;
}
template<typename T, typename U, typename... V>
auto getField(T &input, U (T::*field), V... args)
-> decltype(getField(input.*field, args...))
{
return getField(input.*field, args...);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.