将新的构造函数添加到专用模板类
我有一个类别定义固定长度n
的类。
template<int n>
struct array_container{
/* some code here */
int array[n];
};
假设我想将一个构造函数添加到array_container&lt; 3&gt;
,符合以下方面的内容:
array_container<3>::array_container(int a0, int a1 ,int a2){
array[0] = a0;
array[1] = a1;
array[2] = a1;
}
我知道有两种方法:
一个是复制通用类的整个代码,替换<<<<<代码> n 带3,并添加我的构造函数:
template<>
struct array_container<3>{
/* some code here */
int array[3];
array_container(int a0, int a1 ,int a2){
array[0] = a0;
array[1] = a1;
array[2] = a1; }
};
这可以正确起作用,但缺点,需要从通用基础复制所有代码和方法。
另一种方法是在通用类中添加构造函数array_container(int a0,int a1,int a2);
,然后定义:
template<>
array_container<3>:: array_container(int a0, int a1 ,int a2){
array[0] = a0;
array[1] = a1;
array[2] = a1; }
这具有与最佳不确定或在最佳dy处填充我的通用基础类别的缺点最坏的不正确的构造函数,例如 array_container&lt; 2&gt;(int a0,int a1,int a2)
(取决于我是否将定义添加到通用基础中)。
是否有任何方法可以避免这两个陷阱? IE。不需要复制整个通用基本代码以进行专业化,也不需要在通用基础上添加不必要的构造函数?
I have a class defining an array of fixed length n
, with some methods.
template<int n>
struct array_container{
/* some code here */
int array[n];
};
Let's say I want to add a constructor to array_container<3>
, something along the lines of:
array_container<3>::array_container(int a0, int a1 ,int a2){
array[0] = a0;
array[1] = a1;
array[2] = a1;
}
I know of two ways to do this:
One is to copy the entire code of the generic class, replacing n
with 3, and add my constructor:
template<>
struct array_container<3>{
/* some code here */
int array[3];
array_container(int a0, int a1 ,int a2){
array[0] = a0;
array[1] = a1;
array[2] = a1; }
};
This works correctly, but has the disadvantage of needing to copy all the code and methods from the generic base.
Another method is to add a constructor array_container(int a0, int a1, int a2);
in the generic class, then define:
template<>
array_container<3>:: array_container(int a0, int a1 ,int a2){
array[0] = a0;
array[1] = a1;
array[2] = a1; }
This has the disadvantage of populating my generic base class with at best undefined or at worst incorrect constructors, such asarray_container<2>(int a0, int a1 ,int a2)
(undefined or incorrect depending on whether or not I add the definition to the generic base or not).
Is there any approach that avoids both pitfalls? Ie. doesn't need to copy-paste the entire generic base code for the specialization, and doesn't add unnecessary constructors to the generic base?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为什么不简单地使用
std :: array
允许汇总初始化,甚至可以推论大小,以便您可以简单地编写您可以为自己的类添加扣除指南,但是为什么重新实现
array
?Why not simply use
std::array
allows aggregate initialization and even deduces the size so you can simply writeYou can add a deduction guideline for your own class but why re-implement
array
?忽略@goswin von von Brederlow提到的事实,您似乎正在重新发明轮子(
std :: array
and code>和汇总初始化),C ++ 20的需要表达允许您在一个中定义构造函数仅限于某些专业的主要模板。例如:Ignoring the fact that, as @Goswin von Brederlow mentions, you seem to be reinventing the wheel (
std::array
and aggregate initialization), C++20's requires-expressions allows you to define constructors in a primary template that are constrained to only certain specializations. E.g.:更容易的解决方案是用阵列构造它(原始)。
否则,您可能会弄乱variadic模板和参数解开包装。
The easier solution would be to construct it with an array(in place).
Otherwise you can mess with variadic templates and parameter unpacking.
如果碰巧具有C ++ 17编译器,则可以使用以下代码:
并使用它像
等参数数量推导的大小。
构造函数一样从构建器(例如a
看到它活在 Godbolt 。
If happen to have a C++17 compiler you can use the following code:
and use it like
or even let the size be deduced from the number of arguments like
The constructor is a variadic template, taking any number of
int
arguments (the latter is enforced by thestd::enable_if_t
template parameter) and initializes thearray
member from them. A user defined deduction guide can be used to automatically deduce then
parameter from the number of arguments, that you pass to the constructor.See it live on godbolt.