如下代码:
struct A
{
using this_t = A;
template <class F1>
void ins(const F1& fu)
{
}
template <class F1, class... Args>
void ins(const F1& fu, const Args&... args)
{
this_t::ins<Args...>(args...);
// the line which is causing the error:
constexpr decltype(&this_t::ins<Args...>) func = &this_t::ins<Args...>;
}
};
int main()
{
A a;
a.ins(5, 5, 5, 5, 5);
}
给出此错误:
error: reference to overloaded function could not be resolved
如此处所示:https://godbolt.org/z/nJiY4A
这里出了什么问题,然后怎么可能获得指向函数的指针?
我想我知道为什么该行无法编译。它与重载解析无关,而与以下内容有关decltype
:
[over.over] / 1在某些情况下,使用不带参数的重载函数名可以解析为函数,重载集中特定函数的函数指针或成员函数指针。在这种情况下,功能模板名称被认为是命名一组重载功能。如果(可能在应用了函数指针转换(7.13)之后)等于,则为上下文中所需的目标类型
F
的函数类型选择具有类型的函数。目标可以是FT
F
FT
(1.1)-初始化的对象或引用(11.6、11.6.3、11.6.4),
(1.2)-赋值的左侧(8.18),
(1.3)-函数的参数(8.2.2) ,
(1.4)-用户定义的运算符的参数(16.5),
(1.5)-函数,运算符或转换的返回值(9.6.3),
(1.6)-显式类型转换(8.2。 3、8.2.9、8.4)或
(1.7)-非类型模板参数(17.3.2)。重载的函数名称可以在&运算符之前。在没有列出的上下文中,没有参数的情况下,不能使用重载的函数名。
强调我的。仅在那些枚举上下文之外(例如in中)提及重载的函数名decltype(&this_t::ins<F2, Args...>)
会使程序格式错误。通过显式拼写类型来避免这种有问题的用法,使代码可以编译:
void (this_t::*func)(const Args&...) = &this_t::ins<Args...>;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句