【发布时间】:2018-03-27 21:41:19
【问题描述】:
我写了以下隐藏成员函数模板的代码。
#include <iostream>
struct A
{
template<int> void func()
{
std::cout<<"Function tamplate of A"<<std::endl;
}
};
struct B : A
{
template<char> void func()
{
std::cout<<"Function tamplate of B"<<std::endl;
}
using A::func;
};
int main()
{
B().func<0>();
}
这个程序在 Clang 编译器中工作。 Live demo Clang
但是,GCC 编译器给出了歧义错误。Live demo GCC
source_file.cpp: In function ‘int main()’:
source_file.cpp:22:17: error: call of overloaded ‘func()’ is ambiguous
B().func<0>();
那么,哪个编译器是正确的?
【问题讨论】:
-
另外,既然你有
using A::func,你根本没有做任何隐藏。相反,您可以在B类中完整查看 both 模板函数。 -
最后,您忘记将代码保存在 rextester 上。您现在拥有的链接是每个编译器的通用链接。
-
有趣。我试图在这方面查找标准。但它仅指定应在模板参数上进行隐式转换以使其适合参数。没有关于对转化进行排名的问题。很有意思。如果还没有缺陷报告(或者我错过了标准草案中的某些内容),则可以作为缺陷报告的基础。
-
@MarekR 不,这就是您将编译时常量作为模板参数传递给模板的方式。
-
即this尤其是T.C.的评论值得一读
标签: c++ c++11 templates gcc clang