模版小结2
2015-07-28 10:49
281 查看
// test2.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <string>
#include <vector>
#include <iostream>
template<class T, T v>
struct integral_constant {
static const T value = v;
typedef T value_type;
typedef integral_constant<T, v> type;
};
template <class T, T v> const T integral_constant<T, v>::value;
typedef integral_constant<bool, true> true_type;
typedef integral_constant<bool, false> false_type;
template<class> struct is_array : public false_type {};
template<class T, size_t n> struct is_array<T
> : public true_type {};
template<class T> struct is_array<T[]> : public true_type {};
template<class T>
class CT;
template<class T>
class CT<T[]>
{
public:
CT(T* t):_t(t)
{
std::cout << "CT<T[]>" << std::endl;
}
T operator[](int n)
{
return _t
;
}
T* _t;
};
template<class T, int n>
class CT<T
>
{
public:
CT(T* t):_t(t)
{
std::cout << "CT<T
>" << std::endl;
}
T operator[](int n)
{
return _t
;
}
T* _t;
};
int _tmain(int argc, _TCHAR* argv[])
{
bool bArray = is_array<int[]>::value;
bArray = is_array<int[3]>::value;
int ar[10] = {1, 2, 4, 5, 6 , 6,6, 9};
CT<int[]> t(&ar[0]);
for(int i = 0; i < 10; i++)
{
std::cout << t[i] << " ";
}
std::cout << std::endl;
CT<int[12]> t2(&ar[0]);
for(int i = 0; i < 10; i++)
{
std::cout << t2[i] << " ";
}
return 0;
}
//
#include "stdafx.h"
#include <string>
#include <vector>
#include <iostream>
template<class T, T v>
struct integral_constant {
static const T value = v;
typedef T value_type;
typedef integral_constant<T, v> type;
};
template <class T, T v> const T integral_constant<T, v>::value;
typedef integral_constant<bool, true> true_type;
typedef integral_constant<bool, false> false_type;
template<class> struct is_array : public false_type {};
template<class T, size_t n> struct is_array<T
> : public true_type {};
template<class T> struct is_array<T[]> : public true_type {};
template<class T>
class CT;
template<class T>
class CT<T[]>
{
public:
CT(T* t):_t(t)
{
std::cout << "CT<T[]>" << std::endl;
}
T operator[](int n)
{
return _t
;
}
T* _t;
};
template<class T, int n>
class CT<T
>
{
public:
CT(T* t):_t(t)
{
std::cout << "CT<T
>" << std::endl;
}
T operator[](int n)
{
return _t
;
}
T* _t;
};
int _tmain(int argc, _TCHAR* argv[])
{
bool bArray = is_array<int[]>::value;
bArray = is_array<int[3]>::value;
int ar[10] = {1, 2, 4, 5, 6 , 6,6, 9};
CT<int[]> t(&ar[0]);
for(int i = 0; i < 10; i++)
{
std::cout << t[i] << " ";
}
std::cout << std::endl;
CT<int[12]> t2(&ar[0]);
for(int i = 0; i < 10; i++)
{
std::cout << t2[i] << " ";
}
return 0;
}
相关文章推荐
- MFC中获得对话框控件相对于父窗口的位置
- 求约数 hdu2601 An easy problem
- Highcharts帮助文档和实际代码不一致
- 暑假- Trie树-(D - Phone List)
- Android Volley完全解析(一),初识Volley的基本用法
- Matlab--多项式
- 讲解如何在Unity的Inspector面板实现类似摄像机层次遮罩的多选效果
- Swift学习记录(Swift和Object-C混编之可选值类型的选择)
- 如何创建你自己的 Git 服务器
- ORACLE大表中删除部分数据最佳方案
- 深入学习JavaScript中的Rest参数和参数默认值
- linux shell 逐行读取文件行
- poj3903 LIS的优化
- hdu 2734(Quicksum)
- c++中sort()及qsort()的用法总结
- 捋捋所遇见的WdatePicker插件的用法
- orm
- iOS开发遇见的坑之二:工程文件中插件和自身工程命名冲突
- POJ 1442 Black Box
- mfc控件位置坐标获取