【发布时间】:2013-09-22 13:55:29
【问题描述】:
下面,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;
}
这如预期的那样使用 gcc 打印 1 0。然而,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; }
^
即,只能看到Y 的版本。我试过了
using X::template f;
相反,没有成功。非静态(模板)成员函数也是如此。那么这是一个错误吗?
【问题讨论】:
-
X 中有趣的模板
-
注意:我尝试了另一个版本,其中自动推导出模板参数,并且在两个编译器中都可以使用(但我需要明确的说明)。
-
@iavr:另一方面,您定义 main() 的方式不可移植。
-
@thokra 怎么样?如果程序员省略了来自
main()的return 语句,则标准要求C++ 编译器插入return 0;。 -
@PetrBudnik:参数列表呢?
标签: c++ overloading template-function using-declaration member-functions