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

如何編寫類型特征“is_container"或“is_vector&

How to write a type trait `is_container` or `is_vector`?(如何編寫類型特征“is_container或“is_vector?)
本文介紹了如何編寫類型特征“is_container"或“is_vector"?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時送ChatGPT賬號..

是否可以編寫一個值對所有常見 STL 結(jié)構(gòu)(例如,vectorsetmapmap、...)?

首先,我想編寫一個類型特征,它對 vector 為真,否則為假.我試過這個,但它不能編譯:

template結(jié)構(gòu) is_vector {static bool const value = false;};模板<T類,U類>struct is_vector>>::類型>{static bool const value = true;};

錯誤信息是模板參數(shù)未用于部分特化:U.

解決方案

看,另一個基于 SFINAE 的用于檢測類 STL 容器的解決方案:

template結(jié)構(gòu) is_container : std::false_type {};模板結(jié)構(gòu) is_container_helper {};模板struct is_container<,std::conditional_t<錯誤的,is_container_helper<類型名稱 T::value_type,類型名稱 T::size_type,類型名稱 T::allocator_type,類型名 T::iterator,類型名 T::const_iterator,decltype(std::declval().size()),decltype(std::declval().begin()),decltype(std::declval().end()),decltype(std::declval().cbegin()),decltype(std::declval().cend())>,空白>>:公共 std::true_type {};

當(dāng)然,您可以更改要檢查的方法和類型.

如果您只想檢測 STL 容器(這意味著 std::vectorstd::list 等),您應(yīng)該執(zhí)行類似 這個.

更新.正如@Deduplicator 所指出的,容器可能不滿足 AllocatorAwareContainer 要求(例如:std::array).這就是為什么不需要檢查 T::allocator_type 的原因.但是您可以以類似的方式檢查任何/所有Container要求.>

Is it possible to write a type trait whose value is true for all common STL structures (e.g., vector, set, map, ...)?

To get started, I'd like to write a type trait that is true for a vector and false otherwise. I tried this, but it doesn't compile:

template<class T, typename Enable = void>
struct is_vector {
  static bool const value = false;
};

template<class T, class U>
struct is_vector<T, typename boost::enable_if<boost::is_same<T, std::vector<U> > >::type> {
  static bool const value = true;
};

The error message is template parameters not used in partial specialization: U.

解決方案

Look, another SFINAE-based solution for detecting STL-like containers:

template<typename T, typename _ = void>
struct is_container : std::false_type {};

template<typename... Ts>
struct is_container_helper {};

template<typename T>
struct is_container<
        T,
        std::conditional_t<
            false,
            is_container_helper<
                typename T::value_type,
                typename T::size_type,
                typename T::allocator_type,
                typename T::iterator,
                typename T::const_iterator,
                decltype(std::declval<T>().size()),
                decltype(std::declval<T>().begin()),
                decltype(std::declval<T>().end()),
                decltype(std::declval<T>().cbegin()),
                decltype(std::declval<T>().cend())
                >,
            void
            >
        > : public std::true_type {};

Of course, you might change methods and types to be checked.

If you want to detect only STL containers (it means std::vector, std::list, etc) you should do something like this.

UPDATE. As @Deduplicator noted, container might not meet AllocatorAwareContainer requirements (e.g.: std::array<T, N>). That is why check on T::allocator_type is not neccessary. But you may check any/all Container requirements in a similar way.

這篇關(guān)于如何編寫類型特征“is_container"或“is_vector"?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡單指針的區(qū)別?)
Difference between const. pointer and reference?(常量之間的區(qū)別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問向量的內(nèi)容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對普通變量進(jìn)行多態(tài)?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會導(dǎo)致訪問沖突?)
主站蜘蛛池模板: 精品一区二区av | 成人日韩 | 成人在线精品视频 | 午夜免费观看网站 | 亚洲午夜精品一区二区三区 | 久久精品久久精品 | 蜜桃视频麻豆 | 在线观看深夜视频 | 亚洲成人福利在线观看 | 欧美在线视频一区二区 | 日韩av福利在线观看 | 一级欧美黄色片 | 黄色视频a级毛片 | 亚洲精品一区二区在线观看 | 在线国产一区二区三区 | 日韩精品免费视频 | 999视频 | 一区二区三区国产好的精 | 激情欧美日韩一区二区 | 亚洲一区在线日韩在线深爱 | 国产精品99久久久久久动医院 | 欧美群妇大交群中文字幕 | 天堂免费看片 | 盗摄精品av一区二区三区 | 日本黄色的视频 | 国产精品美女久久久 | 国产一区二区免费在线 | 精品国产18久久久久久二百 | 久久一区二区视频 | 亚洲第一免费播放区 | 欧美日韩一区在线播放 | 久久久久久天堂 | 国产精品久久久久久久久久久免费看 | 懂色tv | 婷婷综合网| 最新中文字幕第一页视频 | 都市激情亚洲 | 国产精品 欧美精品 | 亚洲毛片在线 | 久久一本 | 国产一区免费 |