Topic: Operator, inheritence and user casts
Author: Biju Thomas <b.thomas@attglobal.net>
Date: 2000/03/27 Raw View
Thierry Grellier wrote:
>
> below, there is a small program that compiles with gcc 2.8.1 but
> generates the following messages with sun compiler (either 4.2 or 5.0) :
> "indir.cc", line 19: Error: The operation "C<B> == C<B>" is illegal.
>
> Who's right according to standard ?
gcc.
A straight application of the normal name-lookup process will find the
"operator==". The compiler should be able to find that the operator is a
viable function by the application of the conversion operator.
>
> 1 struct A {
> 2 friend int operator==(const A&, const A& a);
> 3 int v;
> 4 };
> 5
> 6 int operator==(const A& a, const A& b) { return a.v == b.v; }
> 7
> 8 struct B : A {
> 9 };
> 10
> 11 template <class T> struct C { operator const T&() { return v; }
> T v; };
> 12
> 13 int
> 14 main() {
> 15 C<B> c;
> 16 B b;
> 17 A a;
> 18
> 19 c == c;
--
Biju Thomas
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Gene Bushuyev" <gbush@my-deja.com>
Date: 2000/03/28 Raw View
"Thierry Grellier" <t.grellier@arexsys.com> wrote in message
news:38DA1FCB.878FCFD2@arexsys.com...
>
> Hello,
>
> below, there is a small program that compiles with gcc 2.8.1 but
> generates the following messages with sun compiler (either 4.2 or 5.0) :
> "indir.cc", line 19: Error: The operation "C<B> == C<B>" is illegal.
> "indir.cc", line 20: Error: The operation "C<B> == B" is illegal.
> "indir.cc", line 21: Error: The operation "C<B> == A" is illegal.
> "indir.cc", line 23: Error: The operation "B == C<B>" is illegal.
> "indir.cc", line 27: Error: The operation "A == C<B>" is illegal.
> 5 Error(s) detected.
>
> Who's right according to standard ?
Sun is wrong. All these comparisons that compiler complains about can be
performed by using your cast in class C which is followed by derived-to-base
conversion 13.3.3.1.4p1.
The only problem you get with your code when you compare const object and
that is no the case here. In real code you would certainly like to make cast
operator const:
template <class T> struct C
{
operator const T&() const { return v; }
...
};
--
Gene Bushuyev
*** If Linux is the answer, then what is the question? ***
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Thierry Grellier <t.grellier@arexsys.com>
Date: 2000/03/24 Raw View
Hello,
below, there is a small program that compiles with gcc 2.8.1 but
generates the following messages with sun compiler (either 4.2 or 5.0) :
"indir.cc", line 19: Error: The operation "C<B> == C<B>" is illegal.
"indir.cc", line 20: Error: The operation "C<B> == B" is illegal.
"indir.cc", line 21: Error: The operation "C<B> == A" is illegal.
"indir.cc", line 23: Error: The operation "B == C<B>" is illegal.
"indir.cc", line 27: Error: The operation "A == C<B>" is illegal.
5 Error(s) detected.
Who's right according to standard ?
Thierry
1 struct A {
2 friend int operator==(const A&, const A& a);
3 int v;
4 };
5
6 int operator==(const A& a, const A& b) { return a.v == b.v; }
7
8 struct B : A {
9 };
10
11 template <class T> struct C { operator const T&() { return v; }
T v; };
12
13 int
14 main() {
15 C<B> c;
16 B b;
17 A a;
18
19 c == c;
20 c == b;
21 c == a;
22
23 b == c;
24 b == b;
25 b == a;
26
27 a == c;
28 a == b;
29 a == a;
30
31 return 0;
32 }
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]