Topic: operator->


Author: rmashlan@r2m.com (Robert Mashlan)
Date: 1996/06/01
Raw View
Hello,

I know this might be an old subject, but can someone explain to me why
the standard was changed to allow a member operator-> in a template
that can may or may not return a pointer to a class type, rather than
allowing for non-member overloaded operator -> template function?

It seems to me that the former is an ugly exception to the language
because it allows a class definition to be generated with technically
illegal code.  I don't see any good reasons to disallow non-member
operator-> as long as the function takes a single class type as an
argument.

rm
---
Robert Mashlan  R2M Software  rmashlan@r2m.com
Internet Resources for Windows Developers http://www.r2m.com/windev/
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1996/06/03
Raw View
rmashlan@r2m.com (Robert Mashlan) writes:

>I know this might be an old subject, but can someone explain to me why
>the standard was changed to allow a member operator-> in a template
>that can may or may not return a pointer to a class type, rather than
>allowing for non-member overloaded operator -> template function?

The general rule requiring operator-> to be a class member function
prevents conversions being performed on the left-hand operand. Allowing
non-member operator-> could allow surprising conversions instead
of error messages. Example:
    T m; // class T has constructor T(int)
    // suppose we allowed global operator->
    U* operator->(const T&); // class U has member x
    int n = ...;
    int i = n->x; // oops, meant m->x
In this case, if the global oeprator-> were allowed, the compiler
would generate T(n)->x, which is probably not what was intended.
Since the global operator-> is not allowed, you get an error.

The alternative would be to make some (other) special rule for operator->.

We also have a general rule for function templates: The code need not
be completely valid except when the function is instantiated. If not
for that rule, it would be impossible to write many kinds of templates;
you would have to write a dummy function template, and provide
specializations for every type of interest.

In addition, not every language rule can be checked in a template,
particularly anything that depends on a type parameter. Consider:

 template<class U, class V> class MyClass {
 public:
     V operator->();
     ...
 }

If type V has a member operator->, this function is OK, otherwise
it isn't. You can't tell until the class is instantiated whether
it might be OK. If you instantiate on some type V that doesn't
have an operator->, class MyClass is still OK if you never use
operator->. It seemed unnecessary to rule out this possibility.
If you attempt to instantiate on an improper class V and use
operator->, you get an error.

Thus, no special rule is needed for template versions of operator->.
The general rules cover all cases.
--
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]