Boost,scoped_ptr中的unspecified_bool_type
2012-02-09 21:56
295 查看
从官网上下的boosthttp://sourceforge.net/projects/boost/files/boost/1.48.0/已经在硬盘里扔了半个月了,一直也没时间碰.终于抽出来点空,从scoped_ptr看起来吧.毕竟智能指针相对熟练一点.scoped_ptr应该是属于最简单,安全的非侵入式智能指针了.auto_ptr赋值与拷贝构造会交出指针的控制权,这个行为真是令人头大,不仅不符合常规习惯,而且会增加智力负担.相比之下,scoped_ptr禁止赋值与拷贝构造,http://www.boost.org/doc/libs/1_48_0/libs/smart_ptr/scoped_ptr.htm#conversions,这样就避免了赋值和拷贝构造出现语义的歧义.scoped_ptr的名字起得也很有意思:在当前的生存周期(scope)内使用资源获取即初始化(http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization).
在看到下面一段代码时:
指向成员函数的指针声明与定义:
指向数据成员的指针声明与定义:
回过头来读这两句代码:
这段代码主要是用来驱动下面这一类代码:
上面的代码会先执行 tptr 的 operator unspecified_bool_type() const ,如果返回0,则根据C++03标准中[ 4.11 ] [ 4.12 ],由 0 隐式转换为 null member pointer value ,再由 null member pointer value 隐式转换为bool类型 false. 如果返回 &this_type::px, 则根据 C++03标准中 [ 4.12 ],由 non-null member pointer value
隐式转换为bool类型 true.
这里发现一个有意思的现象,如下代码:
注1:
之所以采用函数指针这种数据类型而不是采用直接转换成为bool.是因为point to member type是比较安全的数据类型.直接转换成为bool ,从而不会有bool类型与其他整型数据类型之间转换的烦恼.
注2:
C++2003标准[4.11] A null pointer constant (4.10) can be converted to a pointer to member type; the result is the null member pointer value of that type and is
常量空指针(返回0的整型常量表达式)可以转换成为指向成员的指针类型;结果是一个成员指针类型的空指针,用于区别用非空指针的成员指针.
distinguishable from any pointer to member not created from a null pointer constant. Two null member pointer values of the same type shall compare equal. The
两个相同类型的空成员指针相等. 常量空指针与cv限定的成员指针类型之间的转换是一次转换,而不是先转换成为指向成员的指针,然后进行cv限定转换.
conversion of a null pointer constant to a pointer to member of cv-qualified type is a single conversion, and not the sequence of a pointer to member conversion followed by a qualification conversion (4.4).
C++2003标准[4.12] An rvalue of arithmetic, enumeration, pointer, or pointer to member type can be converted to an rvalue of type bool. A zero value, null pointer value, or
以下四种类型的右值可以转换成为bool类型: 算术值,枚举值,指针, 成员指针. 数值0,空指针,空成员指针可以被转换成为false,其他数值转换成为true.
null member pointer value is converted to false; any other value is converted to true.
在看到下面一段代码时:
typedef T * this_type::*unspecified_bool_type; operator unspecified_bool_type() const // never throws { return px == 0? 0: &this_type::px; }彻底晕头了.隐约记着 this_type::* 是指向成员的指针的声明方式.指向成员的指针本来就学的不好,赶紧抽一下时间补一补.
指向成员函数的指针声明与定义:
class T{ public: void fun(){}; int mem; }; typedef void (T::*fun)(); //declaration fun fun1 = &T::fun; //definition T t; (t.*fun1)(); void (T::*fun2)() = &T::fun; //declaration and defintion (t.*fun1)();
指向数据成员的指针声明与定义:
typedef int T::*pmember; //declaration pmember mem1 = &T::mem; //definitiion t.*mem1 = 1; int T::*mem2 = &T::mem; //declaration and definition t.*mem2 = 1;
回过头来读这两句代码:
typedef T * this_type::*unspecified_bool_type; //声明了一个指向本类数据成员的指针类型,指向数据成员的类型为T*;
operator unspecified_bool_type() const //定义类型转换符,由this_type转换成为一种指向数据成员的指针类型,指向的数据成员为T* { return px == 0? 0: &this_type::px; }
这段代码主要是用来驱动下面这一类代码:
bool isNull( boost::scoped_ptr<T>& tptr ) { if( tptr ) return true; return false; }
上面的代码会先执行 tptr 的 operator unspecified_bool_type() const ,如果返回0,则根据C++03标准中[ 4.11 ] [ 4.12 ],由 0 隐式转换为 null member pointer value ,再由 null member pointer value 隐式转换为bool类型 false. 如果返回 &this_type::px, 则根据 C++03标准中 [ 4.12 ],由 non-null member pointer value
隐式转换为bool类型 true.
这里发现一个有意思的现象,如下代码:
cout<<tptr<<endl;如果tptr 中的px不为null, 则输出 1 ,反之,则返回 0. 转换过程与上面的过程一样,最后调用的是 bool 参数的cout.
注1:
之所以采用函数指针这种数据类型而不是采用直接转换成为bool.是因为point to member type是比较安全的数据类型.直接转换成为bool ,从而不会有bool类型与其他整型数据类型之间转换的烦恼.
注2:
C++2003标准[4.11] A null pointer constant (4.10) can be converted to a pointer to member type; the result is the null member pointer value of that type and is
常量空指针(返回0的整型常量表达式)可以转换成为指向成员的指针类型;结果是一个成员指针类型的空指针,用于区别用非空指针的成员指针.
distinguishable from any pointer to member not created from a null pointer constant. Two null member pointer values of the same type shall compare equal. The
两个相同类型的空成员指针相等. 常量空指针与cv限定的成员指针类型之间的转换是一次转换,而不是先转换成为指向成员的指针,然后进行cv限定转换.
conversion of a null pointer constant to a pointer to member of cv-qualified type is a single conversion, and not the sequence of a pointer to member conversion followed by a qualification conversion (4.4).
C++2003标准[4.12] An rvalue of arithmetic, enumeration, pointer, or pointer to member type can be converted to an rvalue of type bool. A zero value, null pointer value, or
以下四种类型的右值可以转换成为bool类型: 算术值,枚举值,指针, 成员指针. 数值0,空指针,空成员指针可以被转换成为false,其他数值转换成为true.
null member pointer value is converted to false; any other value is converted to true.
相关文章推荐
- boost::scoped_ptr用法
- boost学习之-scoped_ptr
- boost :: scoped_ptr的和std ::的unique_ptr的区别 是之间的唯一不同boost::scoped_ptr<T>和std::unique_ptr<T>的事实std::uni
- boost::scoped_ptr<CUser> apUser(new CUser);
- boost.smart_ptr-智能指针scoped_ptr
- unspecified_bool_type 手法
- unspecified_bool_type 手法
- Boost 学习(一) scoped_ptr
- 智能指针boost::scoped_ptr
- Boost智能指针——scoped_ptr
- boost.smart_ptr-智能指针scoped_ptr
- boost.smart_ptr-智能指针scoped_ptr
- boost::scoped_ptr 源码分析
- osg源码中的ref_ptr operator unspecified_bool_type() 指向类数据成员的指针&类转化为另一类型(如自定义的C类转化为int)
- c++智能指针:boost::scoped_ptr,boost::shared_ptr,boost::scoped_array
- unspecified_bool_type 手法
- unspecified_bool_type 手法
- Boost智能指针之scoped_ptr
- boost::scoped_ptr
- 【boost::smatr_ptr】scoped_ptr