ChefZ -- 磨劍錄 (A Coder's Log)

慣看秋月春風 一壺濁酒喜相逢 古今多少事 皆賦談笑中
posts - 42, comments - 3, trackbacks - 0, articles - 6

C++ typename的使用

Posted on 2008-11-12 19:35 chefZ 阅读(579) 评论(0)  编辑 收藏 引用

C++箴言:理解typename的两个含义

先舉實例:

(1) 以下未加 typename 編譯不會通過

template <class T, class Pred = std::less<T>,
          class Alloc = std::allocator<T> >
class range_set
{
public:
    // Define standard types for STL container
    typedef Alloc allocator_type;
    typedef typename Alloc::size_type size_type;
    typedef typename Alloc::difference_type difference_type;
    typedef typename Alloc::pointer pointer;
    typedef typename Alloc::const_pointer const_pointer;
    typedef typename Alloc::reference reference;
    typedef typename Alloc::const_reference const_reference;
    typedef typename Alloc::value_type value_type; ...

(2)

const_iterator erase_helper(typename range_t::iterator pp,
                                const T &ss, const T &ee)
...

mutable typenamerange_t::iterator pr_;
....
        const_iterator(const typename range_t::iterator &p,
                       const typename range_set *pc, const T &v)
            : pr_(p), pc_(pc), value_(v)
        {
            // Check that we are creating a valid iterator
            assert(range_t::const_iterator(pr_) ==
                                        pc_->range_.end() ||
                   (!pc_->compare_(v, pr_->sfirst) &&
                    pc_->compare_(v, pr_->slast)));
        }

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