鱼C论坛

 找回密码
 立即注册
查看: 2608|回复: 0

[技术交流] 一个我觉得很有意思的C++泛型模版

[复制链接]
发表于 2013-9-7 15:51:04 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 练家志 于 2013-9-7 16:34 编辑

#include <iostream>
using namespace std;

template <class T>
/*T mymax(T a,T b)
{
    return a>b?a:b;
}*/
      
template<int N>
struct Fib
{
    enum{Result = Fib<N-1>::Result+Fib<N-2>::Result};
};
template <>
struct Fib<1>
{
    enum{Result=1};
};
template <>
struct Fib<0>
{
    enum{Result=0};
};

template < bool b, typename T1, typename T2 >
struct _type_helper
{
    typedef T2 type;
};
template < typename T1, typename T2 >
struct _type_helper< true, T1, T2 >
{
    typedef T1 type;
};
template < typename T1, typename T2 >
struct large_type
{
    typedef typename _type_helper< ( sizeof(T1) > sizeof(T2) ), T1, T2 >::type type;
};
template < typename T1, typename T2 >
struct small_type
{
    typedef typename _type_helper< ( sizeof(T2) > sizeof(T1) ), T1, T2 >::type type;
};
template < typename T1, typename T2 >
typename small_type<T1,T2>::type auto_min( T1 a, T2 b )
{
    return (a < b ? a : b);
}
template < typename T1, typename T2 >
typename large_type<T1,T2>::type auto_max( T1 a, T2 b )
{
    return a > b ? a : b;
}
int main()
{
     int iFib=Fib<20>::Result;
    cout<<iFib<<endl;
    std::cout << auto_max( 'A', 66 ) << std::endl;
    std::cout << auto_min( 'A', 66 ) << std::endl;
    return 0;
}


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-29 03:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表