c++ template 不同用法详细解析与验证代码


(1)
//
//functional.h
//
 #ifndef RAK_FUNCTIONAL_H
#define RAK_FUNCTIONAL_H

#include <cstddef>
#include <functional>

namespace rak{
                 template <typename Type>
                 struct reference_fix {
                                 typedef Type type;
                                 Type m;
                };

                 template <typename Type>
                 struct reference_fix <Type&>{
                                 typedef Type type;
                };
}

#endif

// stl_example2.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "functional.h"

using namespace rak;

struct yy
{
                 typedef int int_t;
                 typedef float f_t;

};

int _tmain (int argc, _TCHAR* argv[])
{
                reference_fix<int >::type x;  
                x = 190;
                 reference_fix<int *>::type p;
                p = &x;

                printf( "p:%d\n", *p);                           

                system ( "pause" );
                 return 0;
}

输出结果:


(2)
//
//functional.h
//

#ifndef RAK_FUNCTIONAL_H
#define RAK_FUNCTIONAL_H

#include <cstddef>
#include <functional>
#include <iostream>
using namespace std;

namespace rak{
                 template <typename Type>
                 struct reference_fix {
                                 typedef Type type;
                                 Type m;
                };

                 template <typename Type>
                 struct reference_fix <Type&>{
                                 typedef Type type;
                };

                 template <typename Type>
                 struct value_t {
                                value_t ( Type v ) : m_v(v) {}
                                 Type operator() () const {
                                                std::cout << "value_t operator" << std::endl;
                                                 return m_v;
                                }
                                 Type m_v;
                };

                 template <typename Type>
                 inline value_t <Type>
                                value( Type v ){
                                                 return value_t <Type> ( v);
                }
                



}


#endif

// stl_example2.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "functional.h"

using namespace rak;

 
int _tmain (int argc, _TCHAR* argv[])
{
                 value_t<int > a(10);

                printf( "a:%d\n", a());

                printf( "b:%d\n",value(a));
                                
                printf( "c:%d\n",value(150));

                system ( "pause" );
                 return 0;
}

输出结果:




(3)
//
//functional.h
//
#ifndef RAK_FUNCTIONAL_H
#define RAK_FUNCTIONAL_H

#include <cstddef>
#include <functional>
#include <iostream>
using namespace std;

namespace rak{
                 template <typename Type>
                 struct reference_fix {
                                 typedef Type type;
                                 Type m;
                };

                 template <typename Type>
                 struct reference_fix <Type&>{
                                 typedef Type type;
                };

                 template <typename Type>
                 struct value_t {
                                value_t ( Type v ) : m_v(v) {}
                                 Type operator() () const {
                                                std::cout << "value_t operator" << std::endl;
                                                 return m_v;
                                }
                                 Type m_v;
                };

                 template <typename Type>
                 inline value_t <Type>
                                value( Type v ){
                                                 return value_t <Type> ( v);
                }
                
                 template <typename Type, typename Ftor>
                 struct accumulate_t {
                                accumulate_t( Type t , Ftor f) : result( t), m_f(f ) {
                                                cout << "Init accumulate_t:" << t << endl;
                                }
                                
                                 template <typename Arg>
                                 void operator() ( const Arg& a) {result += m_f(a );}
                                
                                 Type result;
                                 Ftor m_f;
                };

                 template <typename Type, typename Ftor>
                 inline accumulate_t <Type, Ftor>
                                accumulate( Type t , Ftor f){
                                                 return accumulate_t <Type, Ftor>( t, f ); //返回一个对象
                }

                 template <typename Type, typename Ftor>
                 struct equal_t {
                                 typedef bool result_type;
                                equal_t( Type t , Ftor f) : m_t( t), m_f(f ){}

                                 template <typename Arg>
                                 bool operator () (Arg & a) {
                                                 return m_t == m_f(a );                                          
                                }
                
                                 Type m_t;
                                 Ftor m_f;
                };

                 template <typename Type, typename Ftor>
                 inline equal_t <Type, Ftor>
                                equal( Type t , Ftor f){
                                 return equal_t <Type, Ftor>( t, f );       //初始化一个equal_t的对象
                }                
}

#endif

// stl_example2.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "functional.h"

using namespace rak;

typedef int (*FunT)(int *m);

int fun(int *m)
{
                 return *m ;
}
 
int _tmain (int argc, _TCHAR* argv[])
{
                 accumulate_t<char , FunT> acl( 'a',fun);
                 int a = 1;
                 int *a0 = &a ;
                cout<< "result:" << acl.result << endl;
                acl(a0);

                cout<< "result1:" << acl.result << endl;                
                 accumulate_t<char , FunT> acl1( 'c',fun);
                acl1 = accumulate( 'd', fun);
                cout << "acl1:" << acl1.result << endl;
                system ( "pause" );
                 return 0;
}
 


输出结果:


(待续......)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值