iav*_*avr 6 c++ overloading member-functions using-declaration template-function
在下面,struct Y重载了X成员函数f.两个重载都是模板函数,但是要明确指定不同的参数(typename和int):
struct X
{
template <typename> static bool f() { return true; }
};
struct Y : public X
{
using X::f;
template <int> static bool f() { return false; }
};
int main()
{
std::cout << Y::f <void>() << " " << Y::f <0>() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
1 0按预期使用gcc 打印.然而,clang(3.3)抱怨说
[...] error: no matching function for call to 'f'
std::cout << Y::f <void>() << " " << Y::f <0>() << std::endl;
^~~~~~~~~~~
[...] note: candidate template ignored: invalid explicitly-specified argument
for 1st template parameter
template <int> static bool f() { return false; }
^
Run Code Online (Sandbox Code Playgroud)
即,只能看到Y版本.我试过了
using X::template f;
Run Code Online (Sandbox Code Playgroud)
相反,没有成功.非静态(模板)成员函数也是如此.这是一个错误吗?
最近根据另一个答案向我解释了这个难题.
来自#clang IRC频道:
[01:16:23] <zygoloid> Xeo: this is a weird corner of the language where clang conforms but the rule is silly
[01:16:31] <Xeo> ... really? :(
[01:16:45] <zygoloid> Xeo: when deciding whether a using-declaration is hidden, we're not allowed to look at the template-parameter-list (nor the return type, iirc)
[01:17:04] <zygoloid> so the derived class declaration of operator()(T) suppresses the using-declaration
[01:17:19] <Xeo> because it has the same signature / parameter types?
[01:17:40] <zygoloid> rigth
Run Code Online (Sandbox Code Playgroud)
解决方法是不在f类中定义uses派生版本.相反,将它移动到一个辅助助手类(在这种情况下,它会引发问题,你认为哪个定义应该获胜).
请参阅此处查看我之前遇到的有问题的案例:Lambda函数作为基类
在这里如何使用额外的基类修复它:
@Xeo无论如何都修复了它,由于某种原因,这个表格对于clang ++ stackoverflow.com/a/18432618/85371并不反感
致谢感谢@Xeo和休息室的人们发掘这个"愚蠢的规则"