Comments (5)
你说得对。这是原作者的练手项目,并不是很严谨。
《STL源码剖析》2.2.3节是这么处理的:
template <class ForwardIterator>
inline void destroy(ForwardIterator first, ForwardIterator last)
{
__destroy(first, last, value_type(first));
}
// 判断元素的数值类型(value type)是否有trivial destructor
template <class ForwardIterator, class T>
inline void __destroy(ForwardIterator first, ForwardIterator last, T*)
{
typedef typename __type_traits<T>::has_trivial_destructor trivial_destructor;
__destroy_aux(first, last, trivial_destructor());
}
//如果元素的数值类型(value type)有non-trivial destructor...
template <class ForwardIterator>
inline void __destroy_aux(ForwardIterator first, ForwardIterator last, __false_type)
{
for (; first < last; ++first)
destroy(&*first);
}
建议对照《STL源码剖析》批判着学。
from tinystl.
嗯,谢谢建议。
其实我也正在根据 SGI STL 造自己的轻量 STL,而作者的 TinySTL 给了我最初的造轮子的勇气和想法;瑕不掩瑜,作者的完成度还是很高的。
from tinystl.
假设ForwardIterator为char,那么_type_traits <ForwardIterator>::is_POD_type为_true_type。
详见TypeTraits.h
template<>
struct _type_traits<char>
{
typedef _true_type has_trivial_default_constructor;
typedef _true_type has_trivial_copy_constructor;
typedef _true_type has_trivial_assignment_operator;
typedef _true_type has_trivial_destructor;
typedef _true_type is_POD_type;
};
from tinystl.
欢迎探讨。
恐怕我的问题表述不清,我再说一下。
我的疑问是:destroy() 这个函数模板,它为了更高效地析构 [first, last) 内的元素,需要对迭代器所指的元素类型进行分类,于是应该以萃取的方式得到元素类型。
_type_traits <ForwardIterator>::is_POD_type
,这句是通过判断迭代器的类型来决定是否调用析构函数。
当 is_POD_type() 为 true_type 和 false_type 时分别执行下列函数模板:
// true_type
template<class ForwardIterator>
inline void _destroy(ForwardIterator first, ForwardIterator last, _true_type){}
// false_type
template<class ForwardIterator>
inline void _destroy(ForwardIterator first, ForwardIterator last, _false_type){
for (; first != last; ++first){
destroy(&*first);
}
}
那么通过迭代器类型来决定是否调用析构函数合理吗?
举个例子,对于 forward_list<int>
来说,其 value_type 为 int(POD ),有 trivial destructor,不需要额外的操作;而通过判断 forward_list<int>::iterator
的类型(non-POD),则需要调用析构函数。
测试代码如下:
#include <type_traits>
#include <forward_list>
#include <iostream>
int main()
{
std::cout << std::boolalpha;
std::cout << "std::is_pod<int>::value = ";
std::cout << std::is_pod<int>::value << std::endl;
std::cout << "std::is_pod<std::forward_list<int>::iterator>::value = ";
std::cout << std::is_pod<std::forward_list<int>::iterator>::value;
return 0;
}
运行结果:
std::is_pod<int>::value = true
std::is_pod<std::forward_list<int>::iterator>::value = false
from tinystl.
学习了
from tinystl.
Related Issues (20)
- 分配器问题 HOT 4
- utility.h 编译报错 HOT 1
- 请问一下你的比较是在同样的编译条件下吗??
- 关于allocate函数的实现与cppreference的冲突
- Construct.h中析构元素中有个小疑问? HOT 1
- alloc::deallocate() 当 bytes > MAXBYTES,直接 free 好像不会释放分配的空间。 HOT 1
- 老哥有兴趣一起合作吗
- 关于Allocator中destroy()函数以及Construct.h中destroy()函数的疑问
- Vector.h中相关移动语义是否需要加上noexpect?
- 我发现的一些bug HOT 1
- 内存池中的内存什么时候释放 HOT 2
- 我认为这行代码应该是有bug HOT 1
- VS2019编译不通过 HOT 5
- 内存分配器malloc内存不足情况存在错误。
- bug in alloc.hpp
- chunk_alloc第二级配置器的问题 HOT 1
- Algorithm.h中快速排序mid3函数的问题
- 反向迭代器的后置++是不是写反了 HOT 1
- 编译报错:“TinySTL::swap”: 对重载函数的调用不明确 HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tinystl.