久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何聲明同一類的成員向量?

How can I declare a member vector of the same class?(如何聲明同一類的成員向量?)
本文介紹了如何聲明同一類的成員向量?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

到底為什么下面這段代碼有效?

Why on earth does the following piece of code work?

struct A {
    std::vector<A> subAs;
};

A 是不完整的類型,對吧?如果有 A*s 的向量,我會理解.但在這里我不明白它是如何工作的.這似乎是一個遞歸定義.

A is an incomplete type, right? If there was a vector of A*s I would understand. But here I don't understand how it works. It seems to be a recursive definition.

推薦答案

這個 paper 被引入 C++17 允許在某些 STL 容器中使用不完整的類型.在此之前,它是未定義的行為.引用論文:

This paper was adopted into C++17 which allows incomplete types to be used in certain STL containers. Prior to that, it was Undefined Behavior. To quote from the paper:

基于 Issaquah 會議的討論,我們實現了共識繼續*采用該方法——不完整的容器類型",但將范圍限制為 std::vectorstd::liststd::forward_list,作為第一步.

Based on the discussion on the Issaquah meeting, we achieved the consensus to proceed* with the approach – "Containers of Incomplete Types", but limit the scope to std::vector, std::list, and std::forward_list, as the first step.

至于標準的變化(重點是我的):

And as for the changes in the standard (emphasis mine):

一個不完整的類型 T 可以在實例化 vector 時使用,如果allocator 滿足allocator-completeness-requirements(17.6.3.5.1).T 應在產生的任何成員之前完成引用了向量的特化.

An incomplete type T may be used when instantiating vector if the allocator satisfies the allocator-completeness-requirements (17.6.3.5.1). T shall be complete before any member of the resulting specialization of vector is referenced.

所以,如果你在實例化 std::vector 時保留默認的 std::allocator,那么根據論文,它將始終使用不完整的類型 T ;否則,這取決于您的 Allocator 是否可以使用不完整的類型 T 進行實例化.

So, there you have it, if you leave the default std::allocator<T> in place when instantiating the std::vector<T, Allocator>, then it will always work with an incomplete type T according to the paper; otherwise, it depends on your Allocator being instantiable with an incomplete type T.

A 是不完整的類型,對吧?如果有 A*s 的向量,我會理解.但在這里我不明白它是如何工作的.這似乎是一個遞歸定義.

A is an incomplete type, right? If there was a vector of A*s I would understand. But here I don't understand how it works. It seems to be a recursive definition.

那里沒有遞歸.以極其簡化的形式,它類似于:

There is no recursion there. In an extremely simplified form, it's similar to:

class A{
    A* subAs;
};

技術上,除了sizecapacity和可能的allocatorstd::vector只需要保持一個指向 A 動態數組的指針,它通過它的分配器管理.(并且指針的大小在編譯時是已知的.)

Technically, apart from size, capacity and possibly allocator, std::vector only needs to hold a pointer to a dynamic array of A it manages via its allocator. (And the size of a pointer is known at compile time.)

因此,實現可能如下所示:

So, an implementation may look like this:

namespace std{

    template<typename T, typename Allocator = std::allocator<T>>
    class vector{

        ....

        std::size_t m_capacity;
        std::size_t m_size;
        Allocator m_allocator;
        T* m_data;
    };

}

這篇關于如何聲明同一類的成員向量?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡單指針的區別?)
Difference between const. pointer and reference?(常量之間的區別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問向量的內容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對普通變量進行多態?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 国产精品久久久久国产a级 欧美日韩国产免费 | 国产亚洲欧美在线视频 | 天天色官网 | 精品视频一二区 | 成人免费视频网站在线看 | 精品久久久久久久久久久久久久 | 久久久久久免费毛片精品 | 亚洲视频观看 | 国产精品免费一区二区 | 成人精品一区二区三区中文字幕 | 精品久久一区 | 亚洲欧洲一区 | 日本色综合 | 综合激情网 | 毛片a区| 一级看片 | 欧美日韩亚洲在线 | 一级欧美 | 国产色99精品9i | 亚洲精品一区中文字幕乱码 | 亚洲成人蜜桃 | av三级在线观看 | 日韩中文视频 | 亚洲精品电影网在线观看 | 日日摸日日碰夜夜爽亚洲精品蜜乳 | 91在线精品秘密一区二区 | 欧美激情视频一区二区三区在线播放 | 亚洲成网 | 欧美日韩免费视频 | 成人免费视频一区二区 | a级黄色片在线观看 | 涩爱av一区二区三区 | 亚洲国产午夜 | 中文字幕免费视频 | 中文字幕在线观看一区 | 日韩视频―中文字幕 | 国产在线精品一区二区三区 | 黄网站免费入口 | 欧美一级在线 | 99tv| 精品国产18久久久久久二百 |