注册 登录
编程论坛 C++教室

读STL中的不解问题

lixang 发布于 2007-01-24 12:20, 765 次点击

读STL中的不解问题
template <class T>
class malloc_allocator
{
public:
typedef T value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;

//问题1:这个红色区域的用的社什么用法,其好处是身么呢?

template <class U> //问题2:这里将其换成template <class T>行么?
struct rebind { typedef malloc_allocator<U> other; };

malloc_allocator() {}
malloc_allocator(const malloc_allocator&) {}
template <class U>
malloc_allocator(const malloc_allocator<U>&) {}
~malloc_allocator() {}

pointer address(reference x) const { return &x; }
const_pointer address(const_reference x) const {
return x;
}

pointer allocate(size_type n, const_pointer = 0) {
void* p = std::malloc(n * sizeof(T));
if (!p)
throw std::bad_alloc();
return static_cast<pointer>(p);
}

void deallocate(pointer p, size_type) { std::free(p); }

size_type max_size() const {
return static_cast<size_type>(-1) / sizeof(T);
}

void construct(pointer p, const value_type& x) {
new(p) value_type(x);
}
void destroy(pointer p) { p->~value_type(); }

private:
void operator=(const malloc_allocator&);
};

template<> //问题3:既然摸板参数为空,那么写不写template<>有什么不同
class malloc_allocator<void> //问题4:malloc_allocator<void><void> 写与不写不同在那里?
{
typedef void value_type;
typedef void* pointer;
typedef const void* const_pointer;

template <class U>
struct rebind { typedef malloc_allocator<U> other; };
};
以上问题望各位朋友给指点指点!

[此贴子已经被作者于2007-1-24 12:29:51编辑过]

7 回复
#2
tyc6112007-01-24 12:36
1. typedef给类型定义别名,其好处是新的类型好记,另外,当改变原类型时,只需要改写这儿的typedef即可,使用这个类的程序不受影响
2. 不行,不得与类的模板参数T重名
3,4没见过这种用法
#3
lixang2007-01-24 13:40
以下是引用tyc611在2007-1-24 12:36:00的发言:
1. typedef给类型定义别名,其好处是新的类型好记,另外,当改变原类型时,只需要改写这儿的typedef即可,使用这个类的程序不受影响
2. 不行,不得与类的模板参数T重名
3,4没见过这种用法

2. 不行,不得与类的模板参数T重名
那么就是说虽然不能使用T(那里使用的是U)
在使用这个成员函数时我可以将T实例为int 同时可以将U实例为int 对么?

#4
tyc6112007-01-24 15:33
以下是引用lixang在2007-1-24 13:40:00的发言:

2. 不行,不得与类的模板参数T重名
那么就是说虽然不能使用T(那里使用的是U)
在使用这个成员函数时我可以将T实例为int 同时可以将U实例为int 对么?

可以

#5
lixang2007-01-24 20:14
以下是引用tyc611在2007-1-24 12:36:00的发言:
1. typedef给类型定义别名,其好处是新的类型好记,另外,当改变原类型时,只需要改写这儿的typedef即可,使用这个类的程序不受影响
2. 不行,不得与类的模板参数T重名
3,4没见过这种用法

1. typedef给类型定义别名,其好处是新的类型好记,另外,当改变原类型时,只需要改写这儿的typedef即可,使用这个类的程序不受影响

那么这里typedef给类型定义别名,使用的作用区域为何?

#6
tyc6112007-01-25 00:58
以下是引用lixang在2007-1-24 20:14:00的发言:

1. typedef给类型定义别名,其好处是新的类型好记,另外,当改变原类型时,只需要改写这儿的typedef即可,使用这个类的程序不受影响

那么这里typedef给类型定义别名,使用的作用区域为何?

In range of the class.

#7
dlcdavid2007-01-27 10:49
以下是引用lixang在2007-1-24 20:14:00的发言:

1. typedef给类型定义别名,其好处是新的类型好记,另外,当改变原类型时,只需要改写这儿的typedef即可,使用这个类的程序不受影响

那么这里typedef给类型定义别名,使用的作用区域为何?

作用域在本类,派生类和友元

#8
dlcdavid2007-01-27 11:07
template<> //问题3:既然摸板参数为空,那么写不写template<>有什么不同 ?
class malloc_allocator<void> //问题4:malloc_allocator<void> 的<void> 写与不写不同在那里?
------------------------------------------------------------------------------------------
这是摸板特化:
template<> //空摸板提示符,格式
class malloc_allocator<void> //尖括号里的是需要特化的摸板形参,即template <class T>中的T的实例
{} //如果T为void构建此特化版本
1