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

C++ 編譯時類型注冊技巧

C++ type registration at compile time trick(C++ 編譯時類型注冊技巧)
本文介紹了C++ 編譯時類型注冊技巧的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我有以下情況:假設我有一堆類型(函子),我想在編譯期間注冊/編譯它們,最好是像 boost::mpl::vector 這樣的東西.你知道有什么技巧可以做得很好嗎?

I have the following situation: suppose I have a bunch of types (functors) which I want to register/compile in during compilation, preferably into something like boost::mpl::vector. Do you know any trick to do so nicely?

我的愿望是擁有實現函子類型和注冊文件的 hpp 文件,其中宏將類型引入編譯.

My desire is to have hpp file which implements functor type and registration file, where a macro brings in type into compilation.

例如

// registered.hpp
REGISTER("functor1.hpp") // implementation
REGISTER("functor2.hpp")
...
boost::mpl::vector<...> types; // full registration vector

希望這是有道理的.謝謝

Hopefully it makes sense. Thank you

推薦答案

有一種方法可以一個一個地注冊類型,然后以 mpl::vector 或類似的形式檢索所有類型.我在 boost 郵件列表上學到了這個技巧(也許是從 Dave Abrahams 那里學到的,雖然我記不清了).

There is a way to register types one by one and then retrieve all of them in the form of mpl::vector or similar. I've learned this trick on the boost mailing lists (perhaps from Dave Abrahams, although I can't recall for sure).

我從 https://github.com/boostcon/2011_presentations/raw/master/thu/Boost.Generic.pdf.

我不會在代碼中使用 MPL 使其自包含.

I won't use MPL in the code to make it self contained.

// The maximum number of types that can be registered with the same tag.
enum { kMaxRegisteredTypes = 10 };

template <int N>
struct Rank : Rank<N - 1> {};

template <>
struct Rank<0> {};

// Poor man's MPL vector.
template <class... Ts>
struct TypeList {
  static const int size = sizeof...(Ts);
};

template <class List, class T>
struct Append;

template <class... Ts, class T>
struct Append<TypeList<Ts...>, T> {
  typedef TypeList<Ts..., T> type;
};

template <class Tag>
TypeList<> GetTypes(Tag*, Rank<0>) { return {}; }

// Evaluates to TypeList of all types previously registered with
// REGISTER_TYPE macro with the same tag.
#define GET_REGISTERED_TYPES(Tag) 
  decltype(GetTypes(static_cast<Tag*>(nullptr), Rank<kMaxRegisteredTypes>()))

// Appends Type to GET_REGISTERED_TYPES(Tag).
#define REGISTER_TYPE(Tag, Type)                              
  inline Append<GET_REGISTERED_TYPES(Tag), Type>::type        
  GetTypes(Tag*, Rank<GET_REGISTERED_TYPES(Tag)::size + 1>) { 
    return {};                                                
  }                                                           
  static_assert(true, "")

使用示例:

struct IntegralTypes;
struct FloatingPointTypes;

// Initially both type lists are empty.
static_assert(std::is_same<GET_REGISTERED_TYPES(IntegralTypes), TypeList<>>::value, "");
static_assert(std::is_same<GET_REGISTERED_TYPES(FloatingPointTypes), TypeList<>>::value, "");

// Add something to both lists.
REGISTER_TYPE(IntegralTypes, int);
REGISTER_TYPE(FloatingPointTypes, float);
static_assert(std::is_same<GET_REGISTERED_TYPES(IntegralTypes), TypeList<int>>::value, "");
static_assert(std::is_same<GET_REGISTERED_TYPES(FloatingPointTypes), TypeList<float>>::value, "");

// Add more types.
REGISTER_TYPE(IntegralTypes, long);
REGISTER_TYPE(FloatingPointTypes, double);
static_assert(std::is_same<GET_REGISTERED_TYPES(IntegralTypes), TypeList<int, long>>::value, "");
static_assert(std::is_same<GET_REGISTERED_TYPES(FloatingPointTypes), TypeList<float, double>>::value, "");

這篇關于C++ 編譯時類型注冊技巧的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 免费视频一区二区三区在线观看 | 国产精品日韩欧美一区二区三区 | 国产免费观看视频 | 亚洲成人激情在线观看 | 九色在线观看 | 久久亚洲欧美日韩精品专区 | 夜夜av| 国产激情视频在线免费观看 | 艹逼网 | 亚洲高清在线观看 | 欧洲精品久久久久毛片完整版 | 精精久久| 成人av观看| 亚洲欧洲成人 | 99re在线| 超碰电影| 亚洲女人天堂网 | 91欧美激情一区二区三区成人 | 亚洲品质自拍视频 | 欧美色影院 | 日本久久久影视 | 国产成人免费视频网站高清观看视频 | 亚洲国产精品一区二区三区 | 国产精品国产成人国产三级 | 男人的天堂一级片 | 国产精品精品视频一区二区三区 | re久久 | 亚洲 中文 欧美 日韩 在线观看 | 午夜影院在线观看免费 | 久久久久一区二区三区 | 中文字幕精品视频 | 一区二区三区四区国产 | 亚洲视频在线看 | 午夜视频在线观看网址 | 欧美日产国产成人免费图片 | 农村妇女毛片精品久久久 | 欧洲精品久久久久毛片完整版 | 色成人免费网站 | 国产精品区二区三区日本 | 精品久久久久久久久久久久 | 日韩欧美一区二区三区免费观看 |