【发布时间】:2015-07-09 18:43:00
【问题描述】:
我正在尝试从基类中专门化一个静态模板函数,并认为这是 typedef/using 语句的一个很好的用例。不过,我似乎无法让它工作。这是非法的,还是我的语法错误?
#include <iostream>
class Base {
public:
template <typename T>
static T func () {
std::cout << (T)3.145 << std::endl;
}
};
class Derived : public Base {
public:
// using derivedFunc = Base::func<int>; // This doesn't work
// typedef Base::func<int> derivedFunc; // Nor this
static constexpr auto derivedFunc = &Base::func<int>; // But this seems to work
};
int main() {
Base::func<double>(); // Prints 3.145
Derived::derivedFunc(); // Prints 3
return 0;
}
【问题讨论】:
标签: templates c++11 static typedef template-specialization