自定义"nullptr",但是代码怎么理解呢?
Custom "nullptr", but how to understand the codes?
来自维基百科,如果我们的编译器不支持c++11,我们可以自己实现一个,如下所示:
const class nullptr_t
{
public:
template<class T>
inline operator T*() const
{ return 0; }
template<class C, class T>
inline operator T C::*() const
{ return 0; }
private:
void operator&() const;
} nullptr = {};
上面的代码我看不懂
--------------------更新-----------------
对不起,我没表达清楚
template<class T>
inline operator T*() const
{ return 0; }
template<class C, class T>
inline operator T C::*() const
{ return 0; }
以上代码,语法我看不懂。
我以前从未见过这种模板形式。(喜欢"operator T C::*()")
template<class T>
inline operator T*() const
{ return 0; }
表示nullptr_t
类型的对象可以隐式转换为任何(非成员)指针类型。它只是为任何类型 T
.
模板化的 operator T *()
函数(转换为类型 T *
)
template<class C, class T>
inline operator T C::*() const
{ return 0; }
表示nullptr_t
类型的对象可以转换为指向任何class(模板参数C
)任何类型(模板参数)的非静态成员的指针T
).
两个运算符都是const
,所以不能改变nullptr_t
对象。
来自维基百科,如果我们的编译器不支持c++11,我们可以自己实现一个,如下所示:
const class nullptr_t
{
public:
template<class T>
inline operator T*() const
{ return 0; }
template<class C, class T>
inline operator T C::*() const
{ return 0; }
private:
void operator&() const;
} nullptr = {};
上面的代码我看不懂
--------------------更新-----------------
对不起,我没表达清楚
template<class T>
inline operator T*() const
{ return 0; }
template<class C, class T>
inline operator T C::*() const
{ return 0; }
以上代码,语法我看不懂。 我以前从未见过这种模板形式。(喜欢"operator T C::*()")
template<class T>
inline operator T*() const
{ return 0; }
表示nullptr_t
类型的对象可以隐式转换为任何(非成员)指针类型。它只是为任何类型 T
.
operator T *()
函数(转换为类型 T *
)
template<class C, class T>
inline operator T C::*() const
{ return 0; }
表示nullptr_t
类型的对象可以转换为指向任何class(模板参数C
)任何类型(模板参数)的非静态成员的指针T
).
两个运算符都是const
,所以不能改变nullptr_t
对象。