RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1529317
Accepted
Mikhailo
Mikhailo
Asked:2023-07-07 04:24:02 +0000 UTC2023-07-07 04:24:02 +0000 UTC 2023-07-07 04:24:02 +0000 UTC

“向量或数组”的概念

  • 772

尝试针对当前情况提出一个概念

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 == intand 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>,所以选择第二种情况。

但在我看来,我在这里也不是这样说的。

那么应该如何呢?

c++
  • 2 2 个回答
  • 73 Views

2 个回答

  • Voted
  1. Best Answer
    user7860670
    2023-07-07T04:32:42Z2023-07-07T04:32:42Z

    就像一个微不足道的特征。

    #include <array>
    #include <vector>
    #include <cstddef>
    
    template<typename T>
    class t_IsVectorOrArray
    {
        public: static constexpr bool value{false};
    };
    
    template<typename Item, ::std::size_t count>
    class t_IsVectorOrArray<::std::array<Item, count>>
    {
        public: static constexpr bool value{true};
    };
    
    template<typename... Args>
    class t_IsVectorOrArray<::std::vector<Args...>>
    {
        public: static constexpr bool value{true};
    };
    
    #include <list>
    static_assert(not t_IsVectorOrArray<int>::value);
    static_assert(not t_IsVectorOrArray<::std::list<int>>::value);
    static_assert(t_IsVectorOrArray<::std::array<int, 4>>::value);
    static_assert(t_IsVectorOrArray<::std::vector<int>>::value);
    

    在线编译器

    存储类型选项:

    #include <array>
    #include <vector>
    #include <cstddef>
    
    template<typename X, typename T>
    class t_IsVectorOrArray
    {
        public: static constexpr bool value{false};
    };
    
    template<typename X, ::std::size_t count>
    class t_IsVectorOrArray<X, ::std::array<X, count>>
    {
        public: static constexpr bool value{true};
    };
    
    template<typename X, typename... Args>
    class t_IsVectorOrArray<X, ::std::vector<X, Args...>>
    {
        public: static constexpr bool value{true};
    };
    
    #include <list>
    static_assert(not t_IsVectorOrArray<int, int>::value);
    static_assert(not t_IsVectorOrArray<int, ::std::list<int>>::value);
    static_assert(not t_IsVectorOrArray<int, ::std::array<float, 4>>::value);
    static_assert(not t_IsVectorOrArray<int, ::std::vector<float>>::value);
    static_assert(t_IsVectorOrArray<int, ::std::array<int, 4>>::value);
    static_assert(t_IsVectorOrArray<int, ::std::vector<int>>::value);
    

    在线编译器

    • 3
  2. Harry
    2023-07-07T22:37:04Z2023-07-07T22:37:04Z

    你来自错误的一边。

    让我们试试这个:你有Container<int,array<int,4>> c4;

    还有三则公告

    template<typename X, typename T>
    class t_IsVectorOrArray
    
    template<typename X, ::std::size_t count>
    class t_IsVectorOrArray<X, ::std::array<X, count>>
    
    template<typename X, typename... Args>
    class t_IsVectorOrArray<X, ::std::vector<X, Args...>>
    

    只要比较一下(不管template)它看起来最像什么?是的,在 上class t_IsVectorOrArray<X, ::std::array<X, count>>,和X是4 在哪里?直 tyutelka 在 tyutelka。intcount

    原则上,它看起来不是这样的class t_IsVectorOrArray<X, ::std::vector<X, Args...>>,这里没有数组。

    还有class t_IsVectorOrArray- 是的,这很好......但不完全在同一个 tyutelka 中,因为第二个参数太复杂了。所以我们选择了这个tyutelka。

    同样,如果我们采用向量,它也会起作用,但已经在向量上了。再次强调:template我们不会对该部分做出反应。它选择什么是它的问题。最主要的是我们有适合我们声明的特定类型。

    如果您编写的内容不适合向量或数组,或者与元素类型和第一个参数的类型不匹配,则没有选项,只有第一个选项适用false。

    是不是更清楚了?

    不是从第一行开始template,而是从第二行开始,从类类型开始。

    • 1

相关问题

  • 编译器和模板处理

  • 指针。找到最小数量

  • C++,关于枚举类对象初始化的问题

  • 函数中的二维数组

  • 无法使用默认构造函数创建类对象

  • C++ 和循环依赖

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5