【发布时间】:2020-10-06 23:42:12
【问题描述】:
我观察到我无法使用类实例 c 执行 &decltype(c)::f 获取指向成员函数 f 的指针,但我可以使用类类型执行 &C::f 获取指向成员函数的指针C,我认为和decltype(c)是一样的。
看这个最小的例子:
struct C{
int f()
{
return 5;
}
} c;
template<typename T, T t, typename S, S*s> void callCf()
{
(s->*t)();
}
int main()
{
callCf<decltype(&decltype(c)::f),&decltype(c)::f,C,&c>();
}
编译得到:
In function 'int main()':
15:3: error: parse error in template argument list
15:58: error: no matching function for call to 'callCf()'
15:58: note: candidate is:
8:49: note: template<class T, T t, class S, S* s> void callCf()
8:49: note: template argument deduction/substitution failed:
15:58: error: template argument 2 is invalid
按预期使用以下工作:
int main()
{
callCf<decltype(&decltype(c)::f),&C::f,C,&c>();
}
即使这样也有效:
int main()
{
using tC = decltype(c);
callCf<decltype(&decltype(c)::f),&tC::f,C,&c>();
}
这也有效:
template<typename T>
struct forward_type{
typedef T type;
};
int main()
{
callCf<decltype(&decltype(c)::f),&forward_type<decltype(c)>::type::f,C,&c>();
}
我的问题是:为什么不能使用 decltype 来获取指向成员函数的指针 &decltype(c)::f?
编辑:@Paul Sanders 在 cmets 中表明最小示例适用于 c++17。我仍在使用c++14。 c++17 是否包含对允许我的最小示例编译的语言的一些更改?
【问题讨论】:
标签: c++ templates c++14 pointer-to-member-functions