void fill_initialize(size_type n, const T& value) {
empty_initialize();
__STL_TRY {
insert(begin(), n, value);
}
__STL_UNWIND(clear(); put_node(node));
}
这是stl中fill_initialize的源代码,其中__STL_TRY和__STL_UNWIND是不是和try、catch的功能类似,为什么__STL_TRY后面的代码用花括号括起来,而__STL_UNWIND后面的代码则用小括号括起来呢
#define __STL_TRY
Definition at line 230 of file stl_config.h.
Referenced by rb_tree::__copy, __uninitialized_copy_copy, __uninitialized_copy_fill, __uninitialized_copy_n, __uninitialized_fill_copy, vector< node *, Alloc >::allocate_and_copy, vector< node *, Alloc >::allocate_and_fill, hashtable::copy_from, rb_tree< key_type, value_type, select1st< value_type >, key_compare, Alloc >::create_node, slist::create_node, list::create_node, deque::deque, slist::fill_initialize, list::fill_initialize, vector::insert, vector::insert_aux, hashtable< Value, Value, HashFcn, identity< Value >, EqualKey, Alloc >::new_node, priority_queue::pop, priority_queue::push, rope::push_front, slist::range_initialize, list::range_initialize, rb_tree< key_type, value_type, select1st< value_type >, key_compare, Alloc >::rb_tree, hashtable::resize, rope::rope, rope::RopeLeaf_from_unowned_char_ptr, temporary_buffer::temporary_buffer, and deque::~deque.
#define __STL_UNWIND ( action )
Definition at line 234 of file stl_config.h.
Referenced by rb_tree::__copy, __uninitialized_copy_copy, __uninitialized_copy_fill, __uninitialized_copy_n, __uninitialized_fill_copy, vector< node *, Alloc >::allocate_and_copy, vector< node *, Alloc >::allocate_and_fill, hashtable::copy_from, rb_tree< key_type, value_type, select1st< value_type >, key_compare, Alloc >::create_node, slist::create_node, list::create_node, deque::deque, slist::fill_initialize, list::fill_initialize, hashtable< Value, Value, HashFcn, identity< Value >, EqualKey, Alloc >::new_node, priority_queue::pop, priority_queue::push, rope::push_front, slist::range_initialize, list::range_initialize, rb_tree< key_type, value_type, select1st< value_type >, key_compare, Alloc >::rb_tree, rope::rope, rope::RopeLeaf_from_unowned_char_ptr, and temporary_buffer::temporary_buffer.
通过宏定义,控制是否使用C++的异常机制,实现起来可能是这样:
1
2
3
4
5
6
7
|
#ifdef __STL_USE_EXCEPTIONS
#define __STL_TRY try
#define __STL_UNWIND(action) catch(...) { action; throw; }
#else
#define __STL_TRY
#define __STL_UNWIND(action)
#endif
|