Cpper
C/C++高级工程师 Android高级软件工程师 IT集成工程师 音频工程师 熟悉c,c++,java,c#,py,js,asp等多种语言 程序猿

有时候我们需要typedef 一种模板
但是c++并没有直接的解决方案
也就是这样是不可行的.
typedef std::vector<T> myvector<T>;

间接的解决方式是:
(该代码源于盖莫游戏引擎代码)

 1     #include <loki/flex/flex_string.h>
 2     typedef flex_string<char> engine_string;
 3     #include <loki/yasli/yasli_vector.h>     
 4     template<class T>      
 5     struct vector_typedef
 6     {
 7         typedef yasli::vector<T,std::allocator<T> > type;
 8     };   
 9     template<class T>      
10     struct list_typedef
11     {
12         typedef yasli::vector<T,std::allocator<T> >           type;       
13     };  

使用的时候是这样的
     list_typedef<int>::type  v;
     list_typedef<Vector3f>::typedef veclist;
     ...
posted on 2010-03-19 12:55 ccsdu2009 阅读(5512) 评论(4)  编辑 收藏 引用
Comments
  • # re: c++模板专题1 typedef template
    expter
    Posted @ 2010-03-19 14:35
    不是用traits就可以了吗?

    支持一下.... good!  回复  更多评论   
  • # re: c++模板专题1 typedef template
    ahfu
    Posted @ 2010-03-23 04:52
    这行写错了吧?
    list_typedef<Vector3f>::typedef veclist;
    是不是应该为:
    list_typedef<Vector3f>::type veclist;  回复  更多评论   
  • # re: c++模板专题1 typedef template[未登录]
    ccsdu2009
    Posted @ 2010-03-23 08:23
    @ahfu
    呵呵 你观察的很仔细 是的  回复  更多评论   
  • # re: c++模板专题1 typedef template
    zw7840
    Posted @ 2012-02-09 15:10
    这行也写错了吧?
    list_typedef<int>::type v;
    应该为
    vector_typedef<int>::type v;  回复  更多评论   

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理