本文主要是介绍User-defined conversion function,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
https://en.cppreference.com/w/cpp/language/cast_operator
在sp的定义里曾经出现过:
inline explicit operator bool () const { return m_ptr != nullptr; }
这样的代码。operator bool就是User-defined conversion function
template<typename T>
class sp {
public:inline sp() : m_ptr(0) { }sp(T* other); // NOLINT(implicit)sp(const sp<T>& other);sp(sp<T>&& other);template<typename U> sp(U* other); // NOLINT(implicit)template<typename U> sp(const sp<U>& other); // NOLINT(implicit)template<typename U> sp(sp<U>&& other); // NOLINT(implicit)~sp();// Assignmentsp& operator = (T* other);sp& operator = (const sp<T>& other);sp& operator = (sp<T>&& other);template<typename U> sp& operator = (const sp<U>& other);template<typename U> sp& operator = (sp<U>&& other);template<typename U> sp& operator = (U* other);//! Special optimization for use by ProcessState (and nobody else).void force_set(T* other);// Resetvoid clear();// Accessorsinline T& operator* () const { return *m_ptr; }inline T* operator-> () const { return m_ptr; }inline T* get() const { return m_ptr; }inline explicit operator bool () const { return m_ptr != nullptr; }// OperatorsCOMPARE(==)COMPARE(!=)COMPARE(>)COMPARE(<)COMPARE(<=)COMPARE(>=)private: template<typename Y> friend class sp;template<typename Y> friend class wp;void set_pointer(T* ptr);T* m_ptr;
};
这篇关于User-defined conversion function的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!