delem< t,n>源自Belem< t>和dcontainer< delem< t,n>>源自bcontainer< belem< t>>如何编码?

发布于 2025-01-23 04:11:15 字数 878 浏览 2 评论 0原文

这个问题很容易在代码中解释。

我已经编码了几个模板类,它们是从唯一的模板类中得出的:

template<typename T,unsigned N> 
struct DElem : 
    public BElem<T> 
{};

我的问题是在我必须从基类的容器中编码这些前派生类型的容器时出现的:

template<typename T, unsigned N>
struct DContainer<DElem<T,N>> : 
    public BContainer<BElem<T>>
{};

在我的具体情况下,可以是std :: tuple容器或std ::数组。

我的第一个近似值是:

template<typename T, T B, std::size_t N>
struct DContainer : 
    public std::array<BElem<T>,N> 
{

   // This container is to hold **DElem<T,B>**
   // 
   // This class has to do a cast for every
   // 
   // **DElem<T,B>** (that is what the DContainer holds) 
   //
   // to **BElem\<T\>**
   // 
   // *If this task is easy I don't found the way*

};

有人有一个想法更简单地完成这些任务还是更适合其他设计?

The question is easy to explain in code.

I have coded several template classes that they derive from a unique template class:

template<typename T,unsigned N> 
struct DElem : 
    public BElem<T> 
{};

My problem arises when I have to code a container of these anterior derived types from a container of the base class:

template<typename T, unsigned N>
struct DContainer<DElem<T,N>> : 
    public BContainer<BElem<T>>
{};

In my concrete case, Container could be std::tuple or std::array.

My first approximation is:

template<typename T, T B, std::size_t N>
struct DContainer : 
    public std::array<BElem<T>,N> 
{

   // This container is to hold **DElem<T,B>**
   // 
   // This class has to do a cast for every
   // 
   // **DElem<T,B>** (that is what the DContainer holds) 
   //
   // to **BElem\<T\>**
   // 
   // *If this task is easy I don't found the way*

};

Someone has an idea to do these tasks more easy or some other design more appropiate?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心在旅行 2025-01-30 04:11:15

你不幸。 的A 容器 delem&lt; t,n&gt;无法代替belem&lt; t&gt;容器。如果可以的话,将允许以下废话。

DContainer<T, 10> d10Container;
BContainer<T> & bContainer = d10Container;
DElem<T, 20> d20;
bContainer.push_back(d20); // pushed a d20 into a container of d10

您可以拥有的是belem&lt; t&gt; view

template<typename T>
class BView {
    class iterator {
        using value_type = BElem<T>;
        using reference = BElem<T> &;
        using pointer = BElem<T> *;
        using difference_type = std::ptrdiff_t;
        using iterator_category = std::forward_iterator_tag; // or whatever
        reference operator*();
        pointer operator->();
        // etc...
    };

    virtual iterator begin() = 0;
    virtual iterator end() = 0;

    // no insert / push_back / whatever
};

您可能要隐藏(或deletebelem&lt的分配操作员; t&gt;,因为多态性分配也相当荒谬。

现在,您拥有所有dcontainer&lt; t,n&gt; s的共享基类,而不允许胡说八道。

另外,如果您不需要 Runtime 多态性,则可以为bcontainer定义概念。使用pre-concept clastica 要求href =“ https://stackoverflow.com/a/60450396/2610810”>作为基础:

template<container C, typename T>
concept BContainer = std::derived_from<typename C::value_type, BElem<T>>;

You are out of luck. A container of DElem<T, N> is not substitutable for a container of BElem<T>. If it could, the following nonsense would be allowed.

DContainer<T, 10> d10Container;
BContainer<T> & bContainer = d10Container;
DElem<T, 20> d20;
bContainer.push_back(d20); // pushed a d20 into a container of d10

What you can have is a view of BElem<T>

template<typename T>
class BView {
    class iterator {
        using value_type = BElem<T>;
        using reference = BElem<T> &;
        using pointer = BElem<T> *;
        using difference_type = std::ptrdiff_t;
        using iterator_category = std::forward_iterator_tag; // or whatever
        reference operator*();
        pointer operator->();
        // etc...
    };

    virtual iterator begin() = 0;
    virtual iterator end() = 0;

    // no insert / push_back / whatever
};

You probably want to hide (or delete) the assignment operator of BElem<T>, because polymorphic assignment is also fairly nonsensical.

Now you have a shared base class for all your DContainer<T, N>s which doesn't permit nonsense.

Alternatively, if you don't need runtime polymorphism, you can just define a concept for BContainer. Using the pre-concept Container requirements as a base:

template<container C, typename T>
concept BContainer = std::derived_from<typename C::value_type, BElem<T>>;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文