skip to Main Content

The builtin operator-> is defined as (*p).m, which is just fine for my iterator, so overloading it would just waste my time and the maintainer’s eyes.

Just trying it wouldn’t guarantee portability, and I haven’t been able to find an answer, though I fear that it is no, because apparently nobody has even considered it before.

Update:
I made a minimal test program to actually try this:

struct S { int m;};

struct P
{   auto& operator*() const { return s;}
    auto operator->() const =default;// { return &s;}
    S s;
};

int main()
{   P p;
    p->m;
}

g++ (Debian 8.3.0-6) compiles this only without =default;//, so seems like defaulting or omitting the overload won’t be portable for years at least.

2

Answers


  1. Chosen as BEST ANSWER

    As Anoop Rana pointed out, only special member functions can be defaulted, and, as Yksisarvinen said, the builtin operator-> exists only for builtin types.

    Redundancy in overloaded operators is a long acknowledged problem. Boost::Operators provides common overloads with CRTP, including operator-> that mimics the builtin behavior:

    #include <boost/operators.hpp>
    
    struct S { int m;};
    
    struct P : boost::dereferenceable< P, const S*>
    {   auto& operator*() const { return s;}
        S s;
    };
    
    int main()
    {   P p;
        p->m;
    }
    

    Unfortunately spelling out the return type is required. (It shouldn't be required IMHO.) Alone it isn't a big step forward, but it's bundled in commonly needed groups like input_iteratable.


  2. Will built-in operator-> be used if I don’t overload it?

    No, only certain special member functions are implicitly declared for a given class-type(and that too under certain circumstances). And operator-> is not one of them. This can be seen from special members which states:

    The six special members functions described above are members implicitly declared on classes under certain circumstances:

    • Default ctor
    • Dtor
    • Copy ctor
    • Copy assignment
    • Move ctor
    • Move assignment

    (emphasis mine)

    Note in the above list, there is no mention of operator->. This means that if you want to use -> with an object of your class-type then you must overload it explicitly.


    Now, coming to your question about the error that you’re getting.

    compiles this only with the commented out definition, so seems like defaulting or omitting the overload won’t be portable for years at least.

    You’re getting the error because operator-> cannot be defaulted. This can be seen from the same special members documentation which says:

    each class can select explicitly which of these members exist with their default definition or which are deleted by using the keywords default and delete, respectively.

    (emphasis mine)

    Note the emphasis on "these members" above. In particular, only the six special members listed above can be defaulted. And again since operator-> is not one of them, it can’t be defaulted using default.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search