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
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:
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
.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:(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.
You’re getting the error because
operator->
cannot be defaulted. This can be seen from the same special members documentation which says:(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 usingdefault
.