【发布时间】:2026-01-05 22:55:01
【问题描述】:
如何减少模板专用类的代码重复?
我正在尝试创建一个类 (MyArray),其行为类似于 std::vector,但在某些函数中接收原始指针作为参数。
这是它的简化版本:-
template<class T> class MyArray{
T database[10];
public: T& get(int index){return database[index];}
void set(int index, T t){
database[index]=t;
}
};
template<class T> class MyArray<std::unique_ptr<T>>{
T* database[10];
public: T*& get(int index){return database[index];}
void set(int index, std::unique_ptr<T> t){
T* tmp=t.release();
database[index]=tmp;
}
};
Here 是一个测试:-
class B{};
int main() {
MyArray<B> test1;
MyArray<B*> test2;
MyArray<std::unique_ptr<B>> test3;
test3.set(2,std::make_unique<B>()));
return 0;
}
问题:请在MyArray中演示一种减少上述代码重复的优雅方法。
我希望的解决方案可能如下所示:-
template<class T> class MyArray{
using U = if(T=std::uniquePtr<X>){X*}else{T};
U database[10];
public: U& get(int index){return database[index];}
void set(int index, T t){
U u = convert(t);//<-- some special function
database[index]=u;
}
};
可能存在一些内存泄漏/损坏。为简单起见,请忽略它。
我只想要一个想法/粗略的指南。 (无需提供完整的可运行代码,但我不介意)
在现实生活中,MyArray 中有 20 多个函数,我希望对许多类进行相同的重构。
编辑:我(次要)编辑了一些代码和标签。感谢 AndyG 和 Jarod42。
【问题讨论】:
-
此代码不是 C++11...
std::make_unique是 C++14 功能。也许您使用的是 Visual Studio 2013 版本? -
不需要
std::move(std::make_unique中的move。 -
@AndyG 鼓掌!我会尽快编辑它。谢谢。
-
@Jarod42 谢谢。这对我来说是新知识。我刚刚注意到我的代码还包含其他错误。我会尝试修复其中的一些。
标签: c++ templates c++14 template-specialization template-classes