(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;
}
(待续......)