在C++文件中只declare class A, 但不以任何方式define class A, 是做什么用?
来源:互联网 发布:vb:是什么意思 编辑:程序博客网 时间:2024/05/18 03:30
1. 只能实例化Foo<int> Foo<char>,因为实例化其他类时,没有定义
#include <iostream>
using namespace std;
template <class T>
class Foo;
template <>
class Foo<int>
{
public:
Foo()
{
cout << "Foo<int>" << endl;
}
};
template <>
class Foo<char>
{
public:
Foo()
{
cout << "Foo<char>" << endl;
}
};
int main()
{
Foo<int> a;
Foo<char> b;
Foo<float> c;
return 0;
}
2. 可以用来区别有相同属性的不同模块或事物。
比如:
class _AudioModule;
class _VideoModule;
template<typename T>
struct SomeAttribute
{
// 这里是属性的一些数据
// 但是在这里并没有用到具体的类型T
}
typedef SomeAttribute<_AudioModule> AudioModule_Attribute
typedef SomeAttribute<_VideoModule> VideoModule_Attribute
也可以理解为定义一些类型吧
Policy Parameter是不需要定义的
3. 可以用于实现类似Loki中的TypeList,这里有一个简单的TypeList,只能用来判断一个类型T是build-in类型还是class类型:
typelist.h
--------------------------------------------------------------------------------
#ifndef TYPELIST_H
#define TYPELIST_H
template <class T, class U>
struct _TypeList {
typedef T first;
typedef U rest;
};
//struct __class_type {
//};
struct __class_type;
struct __true_type {
};
struct __false_type {
};
typedef _TypeList<char,
_TypeList<signed char,
_TypeList<unsigned char,
_TypeList<short,
_TypeList<unsigned short,
_TypeList<int,
_TypeList<unsigned int,
_TypeList<long,
_TypeList<unsigned long,
_TypeList<float,
_TypeList<double,
_TypeList<long double, __class_type> > > > > > > > > > > > PremierList;
template <class T, class L = PremierList>
struct _Find {
static const bool value = _Find<T, typename L::rest>::value;
};
template <class T, class U>
struct _Find<T, _TypeList<T, U>> {
static const bool value = true;
};
template <class T, class U>
struct _Find<T*, U> {
static const bool value = true;
};
template <class T, class U>
struct _Find<const T*, U> {
static const bool value = true;
};
template <int N, class U>
struct _Find<const char [N], U> {
static const bool value = true;
};
template <class T>
struct _Find<T, __class_type> {
static const bool value = false;
};
template <bool isPremier>
struct type_traits;
template <>
struct type_traits<true> {
typedef __true_type isPremier;
};
template <>
struct type_traits<false> {
typedef __false_type isPremier;
};
template <class T>
typename type_traits<_Find<T>::value>::isPremier
is_premier() {
return type_traits<_Find<T>::value>::isPremier();
}
#endif /*TYPELIST_H*/
typelist.cpp
-----------------------------------------------------------------------------
#include "typelist.h"
#include <iostream>
#include <string>
using namespace std;
template <class T>
void fun_impl(const T &s, __true_type) {
cout << "premier type :" << s << endl;
}
template <class T>
void fun_impl(const T &s, __false_type) {
cout << " class type :" << s << endl;
}
template <class T>
void fun(const T &s) {
fun_impl(s, is_premier<T>());
}
int main() {
int i = 100;
int &ri = i;
int *pi = &i;
fun(i);
fun(ri);
fun(pi);
fun(3.15);
fun("a c-style string");
fun(string("a std::string"));
return 0;
}
- 在C++文件中只declare class A, 但不以任何方式define class A, 是做什么用?
- declare a class uncopyable
- Can we declare a class as static
- c++中,class C:public A,B和class C:public A,public B有什么区别
- define a DbParam class and DbParamCollection
- Three ways to define a javascript class
- 3 ways to define a JavaScript class
- 3 ways to define a JavaScript class
- 3-ways-to-define-a-javascript-class/
- 在一个类的头文件(如B.h)中声明了A类,问在此文件中能否用"class A;"来代替#include "A.h"?
- Class a=new Class(){a=b,c=d}。C#语言什么意思 主要是实例化后怎么有大括号从来没这么用过。
- hibernate中使用new class a(class b,class c)查询数据
- JS中class的实现方式,另模拟dojo.declare
- java中的class 与Class , A.class 与 A.getClass() 到底有什么 不同
- How to declare and initialize a static const array as a class member?
- 第六次上机作业Define a concrete class intset&&EOJ2853
- class文件中的字段表集合--field字段在class文件中是怎样组织的
- class文件中的方法表集合--method方法在class文件中是怎样组织的
- 一道BT的面试题之我见
- 开心一笑
- Calendar的使用
- Java中变量、数据类型及运算符的相关理论知识
- 如何在面试中发现优秀程序员
- 在C++文件中只declare class A, 但不以任何方式define class A, 是做什么用?
- 物理引擎
- Network
- 用单例模式实现读取xxx.properties文件的内容
- itoa与atoi的实现
- 生活空间
- 启用网络函数
- 数据结构(C++)顺序表的实现
- Difference between hub, switch and router