【发布时间】:2022-01-11 05:50:53
【问题描述】:
我有一个包含类型和非类型模板参数的模板类。我想专门化一个成员函数,我发现,如下例所示,我可以做一个完整的专门化。
template<typename T, int R>
struct foo
{
foo(const T& v) :
value_(v)
{}
void bar()
{
std::cout << "Generic" << std::endl;
for (int i = 0; i < R; ++i)
std::cout << value_ << std::endl;
}
T value_;
};
template<>
void foo<float, 3>::bar()
{
std::cout << "Float" << std::endl;
for (int i = 0; i < 3; ++i)
std::cout << value_ << std::endl;
}
但是这个部分特化不会编译。
template<int R>
void foo<double, R>::bar()
{
std::cout << "Double" << std::endl;
for (int i = 0; i < R; ++i)
std::cout << value_ << std::endl;
}
有没有办法实现我正在尝试的任何人都知道的事情?我在 MSVC 2010 中尝试过。
【问题讨论】:
标签: c++ templates template-specialization partial-specialization