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

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

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

////////////////////////////////////////////////////////////////////////////////
// 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是一個編譯期常量整數(shù)
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 };
    };
記得以前我看到這個的時候感覺實現(xiàn)的太巧妙了
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個類型之間的轉(zhuǎn)換關系
具體源碼為:
    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類型可以轉(zhuǎn)化為U類型(比如子類到父類)
關于變量exists2Way表達的是U,T類型是否可以雙向轉(zhuǎn)化(比如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類型的公共父類則結(jié)果為真
否則為假
代碼為:
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*可以轉(zhuǎn)化為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 閱讀(2088) 評論(6)  編輯 收藏 引用
Comments
  • # re: Loki技法5-TypeMap
    luoweisong
    Posted @ 2010-04-19 10:58
    很想認真看你的文章,但你的頁面顏色大讓人不舒服了  回復  更多評論   
  • # re: Loki技法5-TypeMap
    空明流轉(zhuǎn)
    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
    @空明流轉(zhuǎn)
    我現(xiàn)在已經(jīng)不大使用boost了
    這個東西大的出奇
    (當然我只在底層使用若干子庫)  回復  更多評論   
  • # re: Loki技法5-TypeMap
    空明流轉(zhuǎn)
    Posted @ 2010-04-19 12:40
    我是說在methodology上。
    至于大不大,我覺得至少boost比loki好,后者幾乎是一個實驗性質(zhì)的東西。  回復  更多評論   
  • # re: Loki技法5-TypeMap
    空明流轉(zhuǎn)
    Posted @ 2010-04-19 12:43
    還有,你換一個template吧。你這個template的顏色實在難看的不行。  回復  更多評論   

只有注冊用戶登錄后才能發(fā)表評論。
網(wǎng)站導航: 博客園   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>
            国产日本欧美在线观看| 国产精品区一区二区三| 亚洲精品日韩综合观看成人91| 免费中文字幕日韩欧美| 久久精品91久久香蕉加勒比| 宅男精品视频| 久久免费视频网| 久久国产精品网站| 午夜精品视频网站| 精品999日本| 欧美日韩网站| 狠狠狠色丁香婷婷综合激情| 亚洲高清资源| 国产自产v一区二区三区c| 国产一区导航| 亚洲国产欧美在线| 亚洲夜间福利| 久久久久久久尹人综合网亚洲| 久久久激情视频| 91久久精品网| 亚洲欧美中文日韩v在线观看| 久久三级福利| 欧美岛国激情| 国产精品成人一区二区网站软件 | 欧美不卡福利| 欧美激情久久久久久| 亚洲一区在线播放| 欧美成人视屏| 国产精品一区二区久久久| 一区二区在线视频观看| 亚洲深夜福利视频| 久久夜色精品国产噜噜av| 亚洲精品美女91| 亚洲色诱最新| 美国成人直播| 国产精品亚洲а∨天堂免在线| 亚洲大胆av| 欧美一级免费视频| 亚洲欧洲午夜| 久热爱精品视频线路一| 国产精品午夜视频| 亚洲精品一区久久久久久| 久久精品噜噜噜成人av农村| 亚洲精品国产品国语在线app| 欧美一区二区三区啪啪| 欧美三级免费| 一本久道综合久久精品| 欧美一区二区在线看| 久久国产福利国产秒拍| 99精品热视频只有精品10| 久久精品国产99| 欧美视频在线不卡| 日韩一级免费| 久久精品中文字幕一区| 蜜臀a∨国产成人精品| 国产精品a久久久久| 在线电影欧美日韩一区二区私密| 亚洲欧美在线x视频| 日韩视频亚洲视频| 欧美成人精品影院| 亚洲高清在线精品| 久久久久一区二区三区四区| 亚洲一级片在线观看| 欧美日韩一区二区三| 9人人澡人人爽人人精品| 亚洲第一福利视频| 欧美在线观看视频在线| 国产精品影视天天线| 午夜电影亚洲| 亚洲专区在线视频| 国产精品亚洲片夜色在线| 日韩亚洲欧美一区| 亚洲日本理论电影| 欧美日韩xxxxx| 在线一区二区视频| 亚洲视频一区二区| 国产女人水真多18毛片18精品视频| 欧美一级成年大片在线观看| 午夜国产一区| 亚洲成人原创| 亚洲欧洲在线观看| 欧美丝袜一区二区三区| 亚洲在线一区二区三区| 欧美亚洲视频在线看网址| 国产有码在线一区二区视频| 欧美α欧美αv大片| 欧美国产国产综合| 午夜国产不卡在线观看视频| 久久久人成影片一区二区三区观看 | **性色生活片久久毛片| 亚洲高清久久久| 欧美午夜精品一区| 久久色中文字幕| 欧美电影打屁股sp| 亚洲欧美在线视频观看| 久久久久欧美精品| 亚洲一区久久久| 久久精品国产亚洲一区二区| 亚洲美女淫视频| 午夜日韩在线| 亚洲国产欧美日韩| 91久久综合| 亚洲国产精品尤物yw在线观看| 欧美日韩国产美| 午夜精品短视频| 久久精品中文字幕一区| 国产亚洲一级| 久久精品夜色噜噜亚洲aⅴ| 女仆av观看一区| 国产精品vvv| 亚洲黄色片网站| 亚洲三级免费| 国产一区二区三区成人欧美日韩在线观看| 欧美第一黄色网| 国产精品自拍视频| 亚洲视频免费在线| 久久午夜电影| 亚洲欧美日韩在线一区| 欧美成人精品h版在线观看| 欧美精品三级| 欧美午夜精品理论片a级按摩| 国产一区二区激情| 欧美性猛交xxxx乱大交蜜桃| 国产一区二区三区高清在线观看| 亚洲精品网站在线播放gif| 欧美中文字幕久久| 中文国产一区| 欧美日韩精品是欧美日韩精品| 极品中文字幕一区| 欧美一级视频精品观看| 99国产精品私拍| 欧美日韩另类国产亚洲欧美一级| 精品av久久久久电影| 欧美影院成年免费版| 亚洲尤物在线视频观看| 欧美性生交xxxxx久久久| 一本久久综合亚洲鲁鲁五月天| 免费高清在线一区| 久热综合在线亚洲精品| 影音欧美亚洲| 欧美黑人一区二区三区| 欧美激情第五页| 亚洲中无吗在线| 国产日韩欧美精品一区| 欧美在线一级va免费观看| 欧美亚洲日本国产| 亚洲国产精品成人综合色在线婷婷 | 久久精品国产亚洲一区二区| 亚洲免费在线观看| 亚洲视频在线一区观看| 久久久久一区| 欧美日韩国产免费| 亚洲人成精品久久久久| 免费精品99久久国产综合精品| 免费成人激情视频| 亚洲精品在线三区| 欧美日韩三级视频| 亚洲一区二区三区欧美| 欧美中文字幕在线观看| 国内精品久久久久久久影视蜜臀 | 国外成人在线视频网站| 久久精品国产2020观看福利| 蜜桃av一区二区三区| 91久久精品久久国产性色也91| 欧美护士18xxxxhd| 亚洲性感美女99在线| 久久久久综合| 亚洲精品久久久久中文字幕欢迎你| 欧美精品色网| 性高湖久久久久久久久| 欧美激情第二页| 亚洲欧美视频一区| 伊人春色精品| 欧美午夜精品理论片a级按摩| 久久爱另类一区二区小说| 亚洲人在线视频| 久久久噜噜噜久久人人看| aa级大片欧美| 在线观看欧美激情| 国产精品国产三级国产aⅴ浪潮| 久久精品国产69国产精品亚洲| 日韩一区二区福利| 美腿丝袜亚洲色图| 亚洲嫩草精品久久| 亚洲精品在线免费| 在线日本欧美| 国产一区久久| 国产精品毛片高清在线完整版| 看片网站欧美日韩| 欧美一区二区三区在线视频 | 久久一本综合频道| 一区二区三区不卡视频在线观看| 蜜桃久久精品乱码一区二区| 亚洲人午夜精品| 欧美亚洲成人网| 久久精品一区二区三区不卡| 久久久国产成人精品| 亚洲成色777777女色窝| 亚洲日韩欧美视频一区| 国产精品一区二区女厕厕|