青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

Cpper
C/C++高級工程師 Android高級軟件工程師 IT集成工程師 音頻工程師 熟悉c,c++,java,c#,py,js,asp等多種語言 程序猿

以前大致看過模板元編程但是并沒有深入進去
現在就拿比較小的Loki庫研究了
本文主要涉及的是其頭文件TypeMapIP.h
1.根據給定常量生成對等枚舉變量

////////////////////////////////////////////////////////////////////////////////
// class template Int2Type
// Converts each integral constant into a unique type
// Invocation: Int2Type<v> where v is a compile-time constant integral
// Defines 'value', an enum that evaluates to v
////////////////////////////////////////////////////////////////////////////////

    template 
<int v>
    
struct Int2Type
    {
        
enum { value = v };
    };
正如所說:v是一個編譯期常量整數
2.類型重定義
////////////////////////////////////////////////////////////////////////////////
// class template Type2Type
// Converts each type into a unique, insipid type
// Invocation Type2Type<T> where T is a type
// Defines the type OriginalType which maps back to T
////////////////////////////////////////////////////////////////////////////////

    template 
<typename T>
    
struct Type2Type
    {
        typedef T OriginalType;
    };
舉例子為:
Type2Type<vector<int> >::OriginalType v;
當前蓋莫引擎就是這樣使用鏈表的O(∩_∩)O~如下:
    #include <loki/flex/flex_string.h>
    typedef flex_string
<char> engine_string;
    #include 
<loki/yasli/yasli_vector.h>     
    template
<class T>      
    
struct vector_typedef
    {
        typedef yasli::vector
<T,std::allocator<T> > type;
    };   
    template
<class T>      
    
struct list_typedef
    {
        typedef yasli::vector
<T,std::allocator<T> > type;       
    };        
#endif 
基本上是模板重定義
3.條件類型重定義
////////////////////////////////////////////////////////////////////////////////
// class template Select
// Selects one of two types based upon a boolean constant
// Invocation: Select<flag, T, U>::Result
// where:
// flag is a compile-time boolean constant
// T and U are types
// Result evaluates to T if flag is true, and to U otherwise.
////////////////////////////////////////////////////////////////////////////////

    template 
<bool flag, typename T, typename U>
    
struct Select
    {
        typedef T Result;
    };
    template 
<typename T, typename U>
    
struct Select<false, T, U>
    {
        typedef U Result;
    };
舉例為:
Select<true,int,char>::Result r;
則r為int 類型
Seclet<false,int,char>::Result r
r為char類型
這樣的例子基本上在所有模板元編程書上都可以看到
當然還有很多代碼,比如DyObjLib庫
4.檢測對象是否為同一類型
    template <typename T, typename U>
    
struct IsSameType
    {
        
enum { value = false };
    };
    
    template 
<typename T>
    
struct IsSameType<T,T>
    {
        
enum { value = true };
    };
記得以前我看到這個的時候感覺實現的太巧妙了
5.Conversion模板
////////////////////////////////////////////////////////////////////////////////
// class template Conversion
// Figures out the conversion relationships between two types
// Invocations (T and U are types):
// a) Conversion<T, U>::exists
// returns (at compile time) true if there is an implicit conversion from T
// to U (example: Derived to Base)
// b) Conversion<T, U>::exists2Way
// returns (at compile time) true if there are both conversions from T
// to U and from U to T (example: int to char and back)
// c) Conversion<T, U>::sameType
// returns (at compile time) true if T and U represent the same type
//
// Caveat: might not work if T and U are in a private inheritance hierarchy.
////////////////////////////////////////////////////////////////////////////////
Conversion模板指出了給定2個類型之間的轉換關系
具體源碼為:
    template <class T, class U>
    
struct Conversion
    {
        typedef Private::ConversionHelper
<T, U> H;
#ifndef __MWERKS__
        
enum { exists = sizeof(typename H::Small) == sizeof((H::Test(H::MakeT()))) };
#else
        
enum { exists = false };
#endif
        
enum { exists2Way = exists && Conversion<U, T>::exists };
        
enum { sameType = false };
    };
_MWERKS是什么宏定義
先不管它
再看其特化形式
    template <class T>
    
struct Conversion<T, T>    
    {
        
enum { exists = 1, exists2Way = 1, sameType = 1 };
    };
    
    template 
<class T>
    
struct Conversion<void, T>    
    {
        
enum { exists = 0, exists2Way = 0, sameType = 0 };
    };
    
    template 
<class T>
    
struct Conversion<T, void>    
    {
        
enum { exists = 0, exists2Way = 0, sameType = 0 };
    };
    
    template 
<>
    
struct Conversion<voidvoid>    
    {
    
public:
        
enum { exists = 1, exists2Way = 1, sameType = 1 };
    };

這里給定了4種特化形式
3個涉及void類型
1個涉及類型相同
可以看出:
如果2個對象類型相同則枚舉變量sameType必定為真
關于枚舉變量exists
如果其為真則說明T類型可以轉化為U類型(比如子類到父類)
關于變量exists2Way表達的是U,T類型是否可以雙向轉化(比如int,和long類型)
6.繼承關系的鑒別
////////////////////////////////////////////////////////////////////////////////
// class template SuperSubclass
// Invocation: SuperSubclass<B, D>::value where B and D are types. 
// Returns true if B is a public base of D, or if B and D are aliases of the 
// same type.
//
// Caveat: might not work if T and U are in a private inheritance hierarchy.
////////////////////////////////////////////////////////////////////////////////
如果B類型是D類型的公共父類則結果為真
否則為假
代碼為:
template <class T, class U>
struct SuperSubclass
{
    
enum { value = (::Loki::Conversion<const volatile U*const volatile T*>::exists &&
                  
!::Loki::Conversion<const volatile T*const volatile void*>::sameType) };
      
    
// Dummy enum to make sure that both classes are fully defined.
    enum{ dontUseWithIncompleteTypes = ( sizeof (T) == sizeof (U) ) };
};

template 
<>
struct SuperSubclass<voidvoid> 
{
    
enum { value = false };
};

template 
<class U>
struct SuperSubclass<void, U> 
{
    
enum { value = (::Loki::Conversion<const volatile U*const volatile void*>::exists &&
                  
!::Loki::Conversion<const volatile void*const volatile void*>::sameType) };
      
    
// Dummy enum to make sure that both classes are fully defined.
    enum{ dontUseWithIncompleteTypes = ( 0 == sizeof (U) ) };
};

template 
<class T>
struct SuperSubclass<T, void> 
{
    
enum { value = (::Loki::Conversion<const volatile void*const volatile T*>::exists &&
                  
!::Loki::Conversion<const volatile T*const volatile void*>::sameType) };
      
    
// Dummy enum to make sure that both classes are fully defined.
    enum{ dontUseWithIncompleteTypes = ( sizeof (T) == 0 ) };
};
其幾種特化形式都與void類型有關
再看其value描述:
    enum { value = (::Loki::Conversion<const volatile void*const volatile T*>::exists &&
                  
!::Loki::Conversion<const volatile T*const volatile void*>::sameType) };
當void*可以轉化為T*同時T*類型不為void*時則value為真
enum{ dontUseWithIncompleteTypes = ( 0 == sizeof (U) ) };
該枚舉變量時防止類型變量存在不完全類型
可以看出在模板元編程中可以使用enum變量來處理和預報錯誤
給定一個例子
#include <iostream>
#include 
<string>
#include 
<Loki/TypeManIP.h>
#include 
<typeinfo>

class A; 
 
int main()
{
    std::cout
<<Loki::SuperSubclass<void,A>::dontUseWithIncompleteTypes<<std::endl; 
    system(
"PAUSE");
    
return EXIT_SUCCESS;
}
編譯器會報出一個194 E:\c++header\Loki\TypeManIP.h invalid application of `sizeof' to incomplete type `A'  錯誤
7.對象嚴格繼承關系的鑒別
////////////////////////////////////////////////////////////////////////////////
// class template SuperSubclassStrict
// Invocation: SuperSubclassStrict<B, D>::value where B and D are types. 
// Returns true if B is a public base of D.
//
// Caveat: might not work if T and U are in a private inheritance hierarchy.
////////////////////////////////////////////////////////////////////////////////

template
<class T,class U>
struct SuperSubclassStrict
{
    
enum { value = (::Loki::Conversion<const volatile U*const volatile T*>::exists &&
                 
!::Loki::Conversion<const volatile T*const volatile void*>::sameType &&
                 
!::Loki::Conversion<const volatile T*const volatile U*>::sameType) };
    
    
// Dummy enum to make sure that both classes are fully defined.
    enum{ dontUseWithIncompleteTypes = ( sizeof (T) == sizeof (U) ) };
};

template
<>
struct SuperSubclassStrict<voidvoid> 
{
    
enum { value = false };
};

template
<class U>
struct SuperSubclassStrict<void, U> 
{
    
enum { value = (::Loki::Conversion<const volatile U*const volatile void*>::exists &&
                 
!::Loki::Conversion<const volatile void*const volatile void*>::sameType &&
                 
!::Loki::Conversion<const volatile void*const volatile U*>::sameType) };
    
    
// Dummy enum to make sure that both classes are fully defined.
    enum{ dontUseWithIncompleteTypes = ( 0 == sizeof (U) ) };
};

template
<class T>
struct SuperSubclassStrict<T, void> 
{
    
enum { value = (::Loki::Conversion<const volatile void*const volatile T*>::exists &&
                 
!::Loki::Conversion<const volatile T*const volatile void*>::sameType &&
                 
!::Loki::Conversion<const volatile T*const volatile void*>::sameType) };
    
    
// Dummy enum to make sure that both classes are fully defined.
    enum{ dontUseWithIncompleteTypes = ( sizeof (T) == 0 ) };
};
以上是代碼段
當然以上2種類型檢測都不能處理私有繼承的關系
下面是簡單的測試例子:

#include <iostream>
#include 
<string>
#include 
<Loki/TypeManIP.h>
#include 
<typeinfo>

class B
{}; 

class D : public B
{
};
 
int main()
{
    std::cout
<<LOKI_SUPERSUBCLASS(B,D)<<std::endl; 
    std::cout
<<LOKI_SUPERSUBCLASS(D,B)<<std::endl; 
    std::cout
<<LOKI_SUPERSUBCLASS(B,int)<<std::endl; 
    std::cout
<<LOKI_SUPERSUBCLASS(long,D)<<std::endl; 
    system(
"PAUSE");
    
return EXIT_SUCCESS;
}
TypemanIp看完了
8.最后我給出我看到的另外一種檢測對象是否含有Vtable的檢測手法:
template<class T>
struct IsVObjImpl {
    
struct IA : public T {
        
int m;
    };
    
struct IB : public T {
        
virtual void F( );
        
int m;
    };
    
enum { v = (sizeof(IA)==sizeof(IB)) };
};
首先定義2個類對象他們都繼承于給定類型
然后一個還有Vtable
一個不含有Vtable
之后偵測其size是否相同
可以看出vtable是不占有子類大小的
測試例子如下:
#include <iostream>
 
template
<class T>
struct IsVObjImpl 
{
    
struct IA : public T {
        
int m;
    };
    
struct IB : public T {
        
virtual void F( );
        
int m;
    };
    
enum { v = (sizeof(IA)==sizeof(IB)) };
};
 
class A
{
   
virtual void v(){}      
}; 

class B
{
   
void v(){}      
}; 

 
int main()
{
    std::cout
<<IsVObjImpl<A>::v<<std::endl;
    std::cout
<<IsVObjImpl<B>::v<<std::endl;
    system(
"PAUSE");
    
return EXIT_SUCCESS;
}

9.一些其他的對象類型測試手法:
// Check if type is const
template <class T>
struct DoIsConst {
    
enum { v=0 };
};

template 
<class T>
struct DoIsConst<const T> {
    
enum { v=1 };
};

template 
<class T>  // Should this be needed?
struct DoIsConst<const T&> {
    
enum { v=1 };
};

template 
<class T>  // Should this be needed?
struct DoIsConst<const T*> {
    
enum { v=1 };
};


/////////////////////////////////////////
// Check if type is a ref or not
template <class T>
struct DoIsRef {
    
enum { v=0 };
};

template 
<class T>
struct DoIsRef<T&> {
    
enum { v=1 };
};


/////////////////////////////////////////
// Check if type is a pointer or not
template <class T>
struct DoIsPointer {
    
enum { v=0 };
};

template 
<class T>
struct DoIsPointer<T*> {
    
enum { v=1 };
};
這些東西都源于DyObj
A C++ framework for binary reusable objects, or plugins. It enables exposing and sharing run-time type information for C++ classes
當然boost中可定可以找到類型代碼的
應該在type_traits中吧?
posted on 2010-04-18 18:28 ccsdu2009 閱讀(2090) 評論(6)  編輯 收藏 引用
Comments
  • # re: Loki技法5-TypeMap
    luoweisong
    Posted @ 2010-04-19 10:58
    很想認真看你的文章,但你的頁面顏色大讓人不舒服了  回復  更多評論   
  • # re: Loki技法5-TypeMap
    空明流轉
    Posted @ 2010-04-19 11:49
    traits類似于不動點,模板類似于lambda算子。整個metaprogramming體系就是一個template functional language。

    boost.mpl+traits才是正道。  回復  更多評論   
  • # re: Loki技法5-TypeMap
    ccsdu2009
    Posted @ 2010-04-19 11:58
    @luoweisong
    我不太懂如何配置顏色
    有空你可以教教我如何設置
      回復  更多評論   
  • # re: Loki技法5-TypeMap
    ccsdu2009
    Posted @ 2010-04-19 11:59
    @空明流轉
    我現在已經不大使用boost了
    這個東西大的出奇
    (當然我只在底層使用若干子庫)  回復  更多評論   
  • # re: Loki技法5-TypeMap
    空明流轉
    Posted @ 2010-04-19 12:40
    我是說在methodology上。
    至于大不大,我覺得至少boost比loki好,后者幾乎是一個實驗性質的東西。  回復  更多評論   
  • # re: Loki技法5-TypeMap
    空明流轉
    Posted @ 2010-04-19 12:43
    還有,你換一個template吧。你這個template的顏色實在難看的不行。  回復  更多評論   

只有注冊用戶登錄后才能發表評論。
網站導航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


 
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            久久久久se| 99精品国产一区二区青青牛奶| 欧美大片在线观看一区| 国产精品久久午夜| 亚洲黄色视屏| 国产在线观看一区| 亚洲女同同性videoxma| 一区二区三区www| 免费日韩一区二区| 蜜桃av久久久亚洲精品| 国产亚洲一区精品| 亚洲在线中文字幕| 一本一本a久久| 欧美成人精品福利| 亚洲国产成人tv| 伊人成人在线| 久久久人成影片一区二区三区| 欧美专区日韩视频| 国产麻豆日韩欧美久久| 亚洲欧美卡通另类91av| 亚洲在线一区二区| 国产精品都在这里| 亚洲视频高清| 欧美亚洲一区| 国产精品入口| 亚洲免费视频网站| 欧美一区二区三区婷婷月色| 欧美日韩三级| 一区二区三区色| 午夜日韩在线| 国产偷国产偷精品高清尤物| 欧美一区二区国产| 久久久一区二区三区| 黄色欧美成人| 欧美高清视频在线播放| 亚洲激情电影在线| 一区二区三区久久网| 欧美午夜免费电影| 亚洲香蕉成视频在线观看| 欧美一区二区精品| 国产精品一二| 欧美在线free| 亚洲黄色影院| 亚洲一区免费| 红桃视频国产一区| 欧美精品免费在线| 一区二区精品| 久久免费精品日本久久中文字幕| 好吊成人免视频| 欧美国产日韩a欧美在线观看| 亚洲日本激情| 午夜精品福利一区二区蜜股av| 国产欧美三级| 久久综合久久综合这里只有精品 | 亚洲国产成人精品久久久国产成人一区 | 欧美日韩国产另类不卡| 亚洲性视频h| 久久综合999| 在线一区二区三区四区五区| 国产精品一区二区在线观看| 久久久久一区二区| 日韩一区二区免费看| 久久狠狠一本精品综合网| 91久久在线播放| 国产精品久久久久久久久免费桃花 | 国产欧美一区二区三区在线看蜜臀| 久久精品成人欧美大片古装| 亚洲国产精品视频| 久久精品国产99| 中日韩午夜理伦电影免费| 国产中文一区二区| 欧美日韩亚洲激情| 久久青青草综合| 一区二区欧美在线| 欧美国产亚洲视频| 欧美一级艳片视频免费观看| 亚洲激情另类| 国产在线乱码一区二区三区| 欧美精品激情在线| 久久精品夜色噜噜亚洲aⅴ| 一区二区三区产品免费精品久久75 | 伊人色综合久久天天| 欧美人与性动交α欧美精品济南到| 欧美一区二视频| 亚洲视频综合| 亚洲欧洲精品一区二区三区| 久久精品99国产精品| 一区二区三区欧美成人| 在线日韩欧美| 国内精品久久久久久影视8| 欧美日本免费| 葵司免费一区二区三区四区五区| 性做久久久久久久久| 99精品视频免费全部在线| 麻豆精品一区二区综合av| 欧美在线网址| 亚洲一区二区三区欧美 | 国内精品一区二区| 欧美午夜精品| 欧美日韩国产二区| 欧美精品一区二区精品网| 久久夜色精品国产欧美乱极品| 午夜精彩国产免费不卡不顿大片| aa级大片欧美| 亚洲黄色片网站| 亚洲国产高清自拍| 欧美激情91| 麻豆九一精品爱看视频在线观看免费| 亚洲主播在线播放| 一区二区三区国产| 亚洲性av在线| 亚洲欧美日韩国产综合精品二区| 亚洲一二三区在线观看| 9l国产精品久久久久麻豆| 91久久久亚洲精品| 亚洲精品久久久久久久久| 亚洲人成人一区二区三区| 亚洲精品欧美专区| 亚洲美女毛片| 一本大道久久a久久综合婷婷 | 欧美刺激性大交免费视频| 另类天堂av| 欧美激情一区二区三区不卡| 欧美福利精品| 亚洲国内高清视频| 日韩亚洲不卡在线| 亚洲一区999| 欧美在线免费观看| 美腿丝袜亚洲色图| 欧美国产日韩一区| 欧美日韩成人在线观看| 国产精品高潮视频| 国语精品中文字幕| 亚洲精品综合精品自拍| 亚洲午夜电影网| 久久久高清一区二区三区| 欧美丰满高潮xxxx喷水动漫| 亚洲精品美女在线观看| 亚洲一区久久| 久久综合激情| 欧美视频专区一二在线观看| 国产精品久久久久久久7电影| 国产女主播一区| 亚洲激情黄色| 欧美在线视频在线播放完整版免费观看| 久久国产夜色精品鲁鲁99| 欧美国产91| 夜夜嗨av一区二区三区四季av| 亚洲欧美日韩在线高清直播| 久久久精品久久久久| 欧美日韩国产高清| 激情婷婷欧美| 欧美亚洲免费| 亚洲国产欧美日韩| 亚洲欧美三级在线| 欧美激情片在线观看| 国内精品视频666| 亚洲视频网站在线观看| 久久艳片www.17c.com| 一本色道**综合亚洲精品蜜桃冫| 欧美在线首页| 国产精品成av人在线视午夜片| 黄色成人片子| 亚洲欧美日韩成人| 亚洲高清视频一区二区| 亚洲一区二区在线| 欧美日韩国产在线播放| 在线观看视频亚洲| 午夜免费电影一区在线观看| 欧美成人有码| 久久国产直播| 国产精品视频精品| 亚洲日韩欧美视频一区| 久久综合国产精品| 午夜精品视频在线| 国产精品乱码人人做人人爱| 亚洲国产精品小视频| 久久国产精品高清| 亚洲桃花岛网站| 欧美日韩在线精品| 亚洲精品一区久久久久久| 免费在线看成人av| 久久都是精品| 国产一区二区视频在线观看| 欧美中文字幕视频| 中文在线不卡| 欧美性猛交xxxx免费看久久久 | 久久久精品网| 午夜精品福利在线观看| 国产精品久久久久av免费| 欧美一区二区三区在线看| 国产精品久久久久三级| 亚洲欧美日韩国产另类专区| 欧美www视频在线观看| 国产亚洲欧美一区二区| 久久久久www| 久久精品一区二区国产| 狠狠干综合网| 欧美国产日韩二区| 欧美69wwwcom|