本文地址:http://blog.youkuaiyun.com/spch2008/article/details/8996339
Exception specifications
void fun() throw(){}
只是一种标识,约定该函数不应该抛出异常。
1. 如果该函数内部有throw语句,即直接抛出异常,则编译出错。
2. 如果该函数内部调用另一个函数,而此函数内部有throw语句,则编译器无法发现,运行时候抛出异常。
Cygnus实现
1. stl_config.h 中配置信息
# ifdef __STL_USE_EXCEPTIONS
# define __STL_TRY try
# define __STL_CATCH_ALL catch(...)
# define __STL_RETHROW throw
# define __STL_NOTHROW throw()
# define __STL_UNWIND(action) catch(...) { action; throw; }
# else
# define __STL_TRY
# define __STL_CATCH_ALL if (false)
# define __STL_RETHROW
# define __STL_NOTHROW
# define __STL_UNWIND(action)
# endif
# if defined(__STL_USE_NAMESPACES) && !defined(__STL_NO_NAMESPACES)
# define __STD std
# define __STL_BEGIN_NAMESPACE namespace std {
# define __STL_END_NAMESPACE }
# define __STL_USE_NAMESPACE_FOR_RELOPS
# define __STL_BEGIN_RELOPS_NAMESPACE namespace std {
# define __STL_END_RELOPS_NAMESPACE }
# define __STD_RELOPS std
# else
# define __STD
# define __STL_BEGIN_NAMESPACE
# define __STL_END_NAMESPACE
# undef __STL_USE_NAMESPACE_FOR_RELOPS
# define __STL_BEGIN_RELOPS_NAMESPACE
# define __STL_END_RELOPS_NAMESPACE
# define __STD_RELOPS
# endif
auto_ptr代码实现
/*
* Copyright (c) 1997
* Silicon Graphics Computer Systems, Inc.
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Silicon Graphics makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*
*/
#ifndef __SGI_STL_MEMORY
#define __SGI_STL_MEMORY
#include <stl_algobase.h>
#include <stl_alloc.h>
#include <stl_construct.h>
#include <stl_tempbuf.h>
#include <stl_uninitialized.h>
#include <stl_raw_storage_iter.h>
// Note: auto_ptr is commented out in this release because the details
// of the interface are still being discussed by the C++ standardization
// committee. It will be included once the iterface is finalized.
#if 0
#if defined(_MUTABLE_IS_KEYWORD) && defined(_EXPLICIT_IS_KEYWORD) && \
defined(__STL_MEMBER_TEMPLATES)
//命名空间
__STL_BEGIN_NAMESPACE
template <class X>
class auto_ptr
{
private:
X* ptr;
mutable bool owns;
public:
typedef X element_type;
//explicit不运行进行隐式转换,即不允许 auto_ptr<A> ptr = new A;
explicit auto_ptr(X* p = 0) __STL_NOTHROW : ptr(p), owns(p) {}
auto_ptr(const auto_ptr& a) __STL_NOTHROW : ptr(a.ptr), owns(a.owns)
{
a.owns = 0;
}
template <class T>
auto_ptr(const auto_ptr<T>& a) __STL_NOTHROW
: ptr(a.ptr), owns(a.owns)
{
a.owns = 0;
}
auto_ptr& operator=(const auto_ptr& a) __STL_NOTHROW
{
if (&a != this)
{
if (owns)
delete ptr;
owns = a.owns;
ptr = a.ptr;
a.owns = 0;
}
//没有返回?
}
//该函数用于支持相关类型之间的相互赋值
//即类型T可以转换成类型X。比如:X基类,T派生类
template <class T>
auto_ptr& operator=(const auto_ptr<T>& a) __STL_NOTHROW
{
if (&a != this)
{
if (owns)
delete ptr;
owns = a.owns;
ptr = a.ptr;
a.owns = 0;
}
//这里没有返回,代码是否正确?
}
~auto_ptr()
{
if (owns)
delete ptr;
}
X& operator*() const __STL_NOTHROW { return *ptr; }
X* operator->() const __STL_NOTHROW { return ptr; }
X* get() const __STL_NOTHROW { return ptr; }
X* release const __STL_NOTHROW { owns = false; return ptr } //没有分号?
};
__STL_END_NAMESPACE
#endif /* mutable && explicit && member templates */
#endif /* 0 */
#endif /* __SGI_STL_MEMORY */
// Local Variables:
// mode:C++
// End:
上述代码有多处不符合逻辑的地方,是代码错误还是理解有问题?
GCC实现
// auto_ptr implementation -*- C++ -*-
// Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// 代码地址:http://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-api-4.5/a00745_source.html
#ifndef _BACKWARD_AUTO_PTR_H
#define _BACKWARD_AUTO_PTR_H 1
template<typename _Tp>
class auto_ptr
{
private:
_Tp* _M_ptr;
public:
/// The pointed-to type.
typedef _Tp element_type;
explicit
auto_ptr(element_type* __p = 0) throw() : _M_ptr(__p) { }
auto_ptr(auto_ptr& __a) throw() : _M_ptr(__a.release()) { }
template<typename _Tp1>
auto_ptr(auto_ptr<_Tp1>& __a) throw() : _M_ptr(__a.release()) { }
auto_ptr& operator=(auto_ptr& __a) throw()
{
reset(__a.release());
return *this;
}
template<typename _Tp1>
auto_ptr& operator=(auto_ptr<_Tp1>& __a) throw()
{
reset(__a.release());
return *this;
}
~auto_ptr() { delete _M_ptr; }
element_type& operator*() const throw()
{
//就相当于一个Assert
_GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
return *_M_ptr;
}
element_type* operator->() const throw()
{
_GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
return _M_ptr;
}
element_type* get() const throw() { return _M_ptr; }
element_type* release() throw()
{
element_type* __tmp = _M_ptr;
_M_ptr = 0;
return __tmp;
}
void reset(element_type* __p = 0) throw()
{
if (__p != _M_ptr)
{
delete _M_ptr;
_M_ptr = __p;
}
}
};
#endif /* _BACKWARD_AUTO_PTR_H */