尝试针对当前情况提出一个概念
template<typename T, typename C>
其中 C 必须是vector<T>
, 或array<T,N>
具有任意N
。自然is_same
是不行的。你能告诉我该怎么做吗?
我可以在这里发布我失败的尝试,但是,在我看来,如果没有它们,我很明显已经尝试过了,这不是做作业的要求:)
添加用户7860670
我确实是这样的:
template<typename T, typename C>
class t_IsVectorOrArray
{
public: static constexpr bool value{false};
};
template<typename T, typename Item, ::std::size_t count>
class t_IsVectorOrArray<T,::std::array<Item, count>>
{
public: static constexpr bool value{is_same_v<T,Item>};
};
template<typename T, typename Args>
class t_IsVectorOrArray<T,::std::vector<Args>>
{
public: static constexpr bool value{is_same_v<T,Args>};
};
template<typename T = double, typename C = vector<T>>
requires (t_IsVectorOrArray<T,C>::value)
struct Container
{
C c;
};
你是认真的吗?
它似乎有效,但我必须删除矢量模板中的三个点。这很关键吗?从理论上讲,非默认分配器会遇到麻烦吗?
用户7860670的第二次添加
正如Yeralash的孩子所说,我明白它们不相交,但为什么它们不相交呢?
template<typename T, typename C>
class t_IsVectorOrArray
{
public: static constexpr bool value{false};
};
template<typename T, ::std::size_t count>
class t_IsVectorOrArray<T,::std::array<T, count>>
{
public: static constexpr bool value{true}; };
template<typename T, typename... Args>
class t_IsVectorOrArray<T,::std::vector<T, Args...>>
{
public: static constexpr bool value{true};
};
template<typename T = double, typename C = vector<T>>
requires (t_IsVectorOrArray<T,C>::value)
struct Container
{
C c;
};
int main()
{
Container<int,vector<int>> c1;
Container<int,vector<double>> c2;
Container<int,array<double,5>> c3;
Container<int,array<int,4>> c4;
Container<int,list<int>> c5;
}
我试图将自己想象成一个编译器。
我懂了
Container<int,vector<int>> c1;
是的,所以在定义中
template<typename T = double, typename C = vector<T>>
requires (t_IsVectorOrArray<T,C>::value)
struct Container
我们已经有了T == int
and C == vector<int>
。我们不需要显示它们,它们是明确指定的。
现在您需要选择三个类别之一
class t_IsVectorOrArray<int,vector<int>>
class t_IsVectorOrArray<int,::std::array<int, count>>
class t_IsVectorOrArray<int,::std::vector<int, vector<int>>>
如果我是一个编译器,那么我会立即扔掉它array
- 它需要某种count
我没有的。保持
class t_IsVectorOrArray<int,vector<int>>
class t_IsVectorOrArray<int,::std::vector<int, vector<int>>>
但第二个定义是某种废话,它也应该被扔掉。第一个仍然是错误的。
我的推理有什么问题吗?
如果我们假设 for 的类型t_IsVectorOrArray<T,C>
尚未派生,那么我们就没有任何东西可以派生它们。
C array
- nen 至少在某种程度上是清楚的......有一个我们不知道的额外模板参数,但我们至少可以从定义中推断出它,例如,
Container<int,array<int,4>> c4;
我们得到
class t_IsVectorOrArray<int,array<int, 4>>
class t_IsVectorOrArray<int,array<int, count>>
class t_IsVectorOrArray<int,vector<int,array<int, 4>>>
我们再次扔掉最后一个废话,在第二个我们可以推导出来count == 4
,然后它通过定义
class t_IsVectorOrArray<int,array<int,4>>
其中参数其实是最简单的,简单int
简单的4,而第一种情况第二个参数是
array<int,4>
,所以选择第二种情况。
但在我看来,我在这里也不是这样说的。
那么应该如何呢?
就像一个微不足道的特征。
在线编译器
存储类型选项:
在线编译器
你来自错误的一边。
让我们试试这个:你有
Container<int,array<int,4>> c4;
还有三则公告
只要比较一下(不管
template
)它看起来最像什么?是的,在 上class t_IsVectorOrArray<X, ::std::array<X, count>>
,和X
是4 在哪里?直 tyutelka 在 tyutelka。int
count
原则上,它看起来不是这样的
class t_IsVectorOrArray<X, ::std::vector<X, Args...>>
,这里没有数组。还有
class t_IsVectorOrArray
- 是的,这很好......但不完全在同一个 tyutelka 中,因为第二个参数太复杂了。所以我们选择了这个tyutelka。同样,如果我们采用向量,它也会起作用,但已经在向量上了。再次强调:
template
我们不会对该部分做出反应。它选择什么是它的问题。最主要的是我们有适合我们声明的特定类型。如果您编写的内容不适合向量或数组,或者与元素类型和第一个参数的类型不匹配,则没有选项,只有第一个选项适用
false
。是不是更清楚了?
不是从第一行开始
template
,而是从第二行开始,从类类型开始。