c++ template partial specialization member function(c++模板部分特化成员函数)
问题描述
我是模板的新手,所以也许这是一件微不足道的事情,但我无法让它工作.我正在尝试获得类成员函数的部分专业化.最短的代码是:
I'm new to templates so maybe this is a trivial thing but I cannot get it to work. I'm trying to get partial specialization of a class member function. The shortest code would be:
template <typename T, int nValue> class Object{
private:
T m_t;
Object();
public:
Object(T t): m_t(t) {}
T Get() { return m_t; }
Object& Deform(){
m_t*=nValue;
return *this;
}
};
template <typename T>
Object<T,0>& Object<T,0>::Deform(){
this->m_t = -1;
return *this;
}
int main(){
Object<int,7> nObj(1);
nObj.Deform();
std::cout<<nObj.Get();
}
我尝试过非成员函数,效果很好.成员函数的完全特化也能正常工作.
I tried with nonmember functions and that's worked fine. What also works fine is full specialization of a member function.
但是,每当我尝试使用部分规范时.的成员函数我得到形式的错误:
But, whenever I try with partial spec. of a member function I get error of the form:
PartialSpecification_MemberFu.cpp(17): error: template argument
list must match the parameter list Object<T,0>& Object<T,0>::Deform().
希望得到任何帮助:-)
Would appreciate any help :-)
推荐答案
你不能部分特化一个成员函数,你必须部分特化整个类.因此你需要类似的东西:
You cannot partially specialize only a single member function, you must partially specialize the whole class. Hence you'll need something like:
template <typename T>
class Object<T, 0>
{
private:
T m_t;
Object();
public:
Object(T t): m_t(t) {}
T Get() { return m_t; }
Object& Deform()
{
std::cout << "Spec
";
m_t = -1;
return *this;
}
};
这篇关于c++模板部分特化成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:c++模板部分特化成员函数


基础教程推荐
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 从 std::cin 读取密码 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07