How does this typedef work?

I watch this lightning fast report cpp video there it shows this piece of code at 0:37

template<typename T, typename cleanup = QScopedPointerDeleter<T>>
class QScopedPointer{

typedef T *QScopedPointer::*RestrictedBool; // how does this work? 
                           //why not QScopedPointer<T> since QScopedPointer is a template?

public:
inline operator RestrictedBool() const 
 {
  return isNull()? Q_NULLPTR : &QScopedPointer::d;
 }

 inline bool isNull() const{ return !d;}

protected:
 T *d;
};

typedef T *QScopedPointer::*RestrictedBool;Is it hard for me to understand what this means?

I created a similar class F, but it does not compile, what's the difference between two typedefs in class QScopedPointerand class F?

 template<typename T>
 class F{
  typedef T *F::*bool;
  public:
   operator bool(){return true;}
 };
+4
source share
1 answer
typedef T *QScopedPointer::*RestrictedBool;

This can be done a bit when we move the stars around:

typedef T* QScopedPointer::* RestrictedBool;
//      ^~~~~~~~~~~~~~~~~~~~ ^~~~~~~~~~~~~~
//       the type             the alias

In C ++ 11, we will write this more clearly, as

using RestrictedBool = T* QScopedPointer::*;

RestrictedBoolhere declared as a type alias T* QScopedPointer::*. This way, it typedef T *F::boolfails because you cannot override bool:) The name is pretty misleading because the type is not boolean.

T* QScopedPointer::* - . T* QScopedPointer<T, cleanup>, , ,

class QScopedPointer {
    operator RestrictedBool() const {
//           ^~~~~~~~~~~~~~
//            this function returns a `RestrictedBool` = `T* QScopedPointer::*`
        return isNull()? Q_NULLPTR : &QScopedPointer::d;
//                                   ^~~~~~~~~~~~~~~~~~
//                                    and this expression has type `T* QScopedPointer::*`
    }

    T *d;
//  ^~~
//   the `d` member has type `T*` in the `QScopedPointer` class.
};

QScopedPointer<T>, QScopedPointer ?

QScopedPointer<T, cleanup> QScopedPointer<T, cleanup> QScopedPointer. . . ++ .

+6

Source: https://habr.com/ru/post/1670779/


All Articles