lundi 25 septembre 2023

Is it good practice to define a inner memeber accessor operator overload for CRTP template?

This occurs to me that I can add

template<typename T>
class ISock {
    public:
    Message Recv()  {
        return static_cast<T*>(this)->Recv();
    };
    ~ISock(){
        static_cast<T*>(this)->~ISock();
    };

    // use the following complementary...
    T* operator->(){
        return static_cast<T*>(this);
    };
    void Other override {
        (*this)->Other();
    }
};

This allows more flexibility in function definition and reduce boilerplates, for example, if I have

class MockSock : ISock<MockSock> {
     public: 
     ...
     void peculiarFunction(int){
     ...
}

Then I may use the peculiarFunction in defining fn(ISock<MockSock> m) or even with if constexpr for specialization. I am wondering whether it's a good practice to

  1. provide a .inner() method
  2. to overload it as operator-> for simplicity in coding.

Aucun commentaire:

Enregistrer un commentaire