public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: Problem with templates and operator overloading
@ 2004-02-02 20:52 Oliver Kullmann
  0 siblings, 0 replies; 3+ messages in thread
From: Oliver Kullmann @ 2004-02-02 20:52 UTC (permalink / raw)
  To: gcc-help

> 
> I'm having trouble with the following piece of c++ code.  Is this a bug
> in gcc or am I doing something wrong?  The code compiles and runs
> without problems in icc.
> 
> GCC Version: 3.3.2
> 
> struct doubler {
>    double operator*(double val) {
>       return 2.0 * val;
>    }
> };
> 
> template<typename T> T operator*(double lhs , T rhs) {
>    return rhs * lhs;
> }
> 
> int main() {
>    doubler a;
>    a * 3.0;
>    return 0;
> }
> 
> Compiling this gives the following error message.
> 
> test3.cc: In function `T operator*(double, T)':
> test3.cc:7: error: `T operator*(double, T) [with T = double]' must have
> an
>    argument of class or enumerated type
> 
> Thanks.
>

I can't find no fault with this code: 

When overload resolution happens (section 13.3 of the Standard),
then according to clause 13.3.1.2 the set of member candidates
for operator * consists of doubler::operator *(double), while the
set of non-member candidates is empty. So overload resolution
should unambiguously choose the member operator of class doubler.

Instead, gcc seems to ignore the member operator, and considers as only
candidate the operator template, resulting in a failing overload
resolution.

To me g++ seems to be wrong.

Oliver

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Problem with templates and operator overloading
  2004-02-02 16:49 Casey Goodlett
@ 2004-02-02 18:30 ` Eljay Love-Jensen
  0 siblings, 0 replies; 3+ messages in thread
From: Eljay Love-Jensen @ 2004-02-02 18:30 UTC (permalink / raw)
  To: cgoodle, gcc-help

Hi Casey,

I recommend taking out your template operator entirely, and put in a specific:

inline double operator * (double lhs, doubler rhs)
{
  return rhs * lhs;
}

Or alternatively (and especially if your operator struct isn't mathematically oriented), avoid overloading operators entirely and use a method invocation instead.

struct doubler
{
  double calc(double val) { return 2.0 * val; }
};

Or a functor invocation.

struct doubler
{
  double operator () (double val) { return 2.0 * val; }
};

HTH,
--Eljay


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Problem with templates and operator overloading
@ 2004-02-02 16:49 Casey Goodlett
  2004-02-02 18:30 ` Eljay Love-Jensen
  0 siblings, 1 reply; 3+ messages in thread
From: Casey Goodlett @ 2004-02-02 16:49 UTC (permalink / raw)
  To: gcc-help

I'm having trouble with the following piece of c++ code.  Is this a bug
in gcc or am I doing something wrong?  The code compiles and runs
without problems in icc.

GCC Version: 3.3.2

struct doubler {
   double operator*(double val) {
      return 2.0 * val;
   }
};

template<typename T> T operator*(double lhs , T rhs) {
   return rhs * lhs;
}

int main() {
   doubler a;
   a * 3.0;
   return 0;
}

Compiling this gives the following error message.

test3.cc: In function `T operator*(double, T)':
test3.cc:7: error: `T operator*(double, T) [with T = double]' must have
an
   argument of class or enumerated type

Thanks.

-- 
Casey Goodlett (cgoodle@clemson.edu)

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2004-02-02 20:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-02-02 20:52 Problem with templates and operator overloading Oliver Kullmann
  -- strict thread matches above, loose matches on Subject: below --
2004-02-02 16:49 Casey Goodlett
2004-02-02 18:30 ` Eljay Love-Jensen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).