健康,快乐,勇敢的宁帅!!

努力、努力、再努力! 没有什么能阻止我对知识的渴望。

 

"C++Templates The Complete Guide"读书笔记----Chapter 3

Chapter 3 Class Templates
1. A clas template is a class that is implemented with one or more type parameters left open
#include <vector>
#include 
<stdexcept>

template 
<typename T>
class Stack {
  
private:
    std::vector
<T> elems;     // elements

  
public:
    
void push(T const&);      // push element
    void pop();               // pop element
    T top() const;            // return top element
    bool empty() const {      // return whether the stack is empty
        return elems.empty();
    }

}
;

template 
<typename T>
void Stack<T>::push (T const& elem)
{
    elems.push_back(elem);    
// append copy of passed elem
}


template
<typename T>
void Stack<T>::pop ()
{
    
if (elems.empty()) {
        
throw std::out_of_range("Stack<>::pop(): empty stack");
    }

    elems.pop_back();         
// remove last element
}


template 
<typename T>
T Stack
<T>::top () const
{
    
if (elems.empty()) {
        
throw std::out_of_range("Stack<>::top(): empty stack");
    }

    
return elems.back();      // return copy of last element
}
2. To use a class template, you pass the open types as template arguments. The class template is the instantiated(and compiled) for these types
3. For class templates, only those menber funcitons that are called are instantiated
This , of course, saves time and space. It has the additional benefit that you can instantiate a class even for those types that cannot perform all the operations of all the menber functions, as long as these member functions are not called.
Note that you have to put whitespace between the two closing  template brackets. If you don't do this, you are using operator>>,which results in a syntax error:
Stack<Stack<int>> intStackStack; //ERROR:>>is not allowed
4. You can specialize calss templates for certain types
template<>
class Stack<std::string> {
  
private:
    std::deque
<std::string> elems;  // elements

  
public:
    
void push(std::string const&);  // push element
    void pop();                     // pop element
    std::string top() const;        // return top element
    bool empty() const {            // return whether the stack is empty
        return elems.empty();
    }

}
;

void Stack<std::string>::push (std::string const& elem)
{
    elems.push_back(elem);    
// append copy of passed elem
}


void Stack<std::string>::pop ()
{
    
if (elems.empty()) {
        
throw std::out_of_range
                (
"Stack<std::string>::pop(): empty stack");
    }

    elems.pop_back();         
// remove last element
}


std::
string Stack<std::string>::top () const
{
    
if (elems.empty()) {
        
throw std::out_of_range
                (
"Stack<std::string>::top(): empty stack");
    }

    
return elems.back();      // return copy of last element
}


ps:也就是所谓的特定版本,模板元编程中的关键。
5. You can partially specialize class templates for certain types
If more than one partial specilalization matches equally well, the declaration is ambiguous
6. You can define default values for class template parameters.These may refer to previous template parameters

posted on 2006-11-26 16:15 ningfangli 阅读(163) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

统计

公告

Dict.CN 在线词典, 英语学习, 在线翻译

常用链接

留言簿(4)

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜