在C++文件中只declare class A, 但不以任何方式define class A, 是做什么用?

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;
}

Sure, here's an implementation of the Vector class template in C++: ```cpp #include <iostream> #include <vector> template <typename T> class Vector { public: Vector() {} Vector(std::size_t size) : data_(size) {} std::size_t size() const { return data_.size(); } T& operator[](std::size_t index) { return data_[index]; } const T& operator[](std::size_t index) const { return data_[index]; } friend std::ostream& operator<<(std::ostream& os, const Vector<T>& vec) { for (std::size_t i = 0; i < vec.size(); ++i) { os << vec[i] << " "; } return os; } friend std::istream& operator>>(std::istream& is, Vector<T>& vec) { std::size_t size; is >> size; vec = Vector<T>(size); for (std::size_t i = 0; i < size; ++i) { is >> vec[i]; } return is; } private: std::vector<T> data_; }; ``` The `Vector` class template can be instantiated with any element type. It has a default constructor and a constructor that takes a size argument to create a vector of a specific size. The `size()` method returns the size of the vector. The `operator[]` method is overloaded to allow indexing of the vector. The `friend` keyword is used to declare the `operator<<` and `operator>>` functions as friend functions that can access the private `data_` member of the `Vector` class. The `operator<<` function outputs the elements of the vector to the output stream, separated by spaces. The `operator>>` function reads in the size of the vector from the input stream, creates a new `Vector` of that size, and reads in the elements of the vector from the input stream. Example usage: ```cpp int main() { Vector<int> vec(3); std::cin >> vec; std::cout << vec << std::endl; return 0; } ``` This program creates a `Vector` of size 3, reads in 3 integers from the input stream to fill the vector, and then outputs the vector to the output stream.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值