POV-Ray : Newsgroups : povray.off-topic : A question about Java generics (not a flame) Server Time
7 Sep 2024 23:25:08 EDT (-0400)
  A question about Java generics (not a flame) (Message 41 to 50 of 63)  
<<< Previous 10 Messages Goto Latest 10 Messages Next 10 Messages >>>
From: Warp
Subject: Re: A question about Java generics (not a flame)
Date: 14 May 2008 06:45:17
Message: <482ac2bc@news.povray.org>
Fredrik Eriksson <fe79}--at--{yahoo}--dot--{com> wrote:
> If you change the set of functions that take objects of type T as a  
> parameter, the instantiation context changes for a template with T as a  
> type parameter, even though the signature of the template remains the same.

  Can you give me a concrete example?

-- 
                                                          - Warp


Post a reply to this message

From: Darren New
Subject: Re: A question about Java generics (not a flame)
Date: 14 May 2008 12:23:53
Message: <482b1219$1@news.povray.org>
Warp wrote:
>   Can you give me a concrete example?

I probably have all the syntax mixed up, and maybe you need to do 
something more complicated with friend functions and everything else, 
but this is my understanding of what they were talking about in the 
paper?  Basically, the environment in which X<T> is expanded is 
different, so the xyz.xx(4) would promote 4 to a long in main1 and not 
main2.


*** A.h

void X<T>(void);

class A {
   void xx(long i) {
     cout << "Alpha!\n";
   }
}


*** X.cpp

void X<T>(void) {
   T xyz;
   xyz.xx(4);
}

*** Main1.cpp

#include <A.h>

void main1(void) {
   X<A>();
}

*** Main2.cpp

#include <A.h>

void A::xx(int i) {
   cout << "Beta!\n";
}

void main2(void) {
   X<A>();
}



-- 
   Darren New / San Diego, CA, USA (PST)
     "That's pretty. Where's that?"
          "It's the Age of Channelwood."
     "We should go there on vacation some time."


Post a reply to this message

From: Fredrik Eriksson
Subject: Re: A question about Java generics (not a flame)
Date: 14 May 2008 15:07:05
Message: <op.ua5rd2kh7bxctx@e6600.bredbandsbolaget.se>
On Wed, 14 May 2008 18:23:54 +0200, Darren New <dne### [at] sanrrcom> wrote:
>
> *** Main2.cpp
>
> #include <A.h>
>
> void A::xx(int i) {
>    cout << "Beta!\n";
> }

This will fail to compile, because you cannot redefine A::xx.

What I had in mind is something like this (untested, for obvious reasons):


// g.hpp
   template <typename T> void g(T t) { /* Do whatever */ }
// end g.hpp

// main.cpp
   export template <typename T> void f();

   int main(){ f<int>(); }
// end main.cpp

// export.cpp
   #include "g.hpp"

   template <typename T> void f(){ g( T() ); }
// end export.cpp


Now make a small change to main.cpp:

// main.cpp
   export template <typename T> void f();

   #include "g.hpp"
   template <> void g(int i) { /* Do something completely different */ }

   int main(){ f<int>(); }
// end main.cpp


The instantiation context for the exported template changes, because there  
is now an additional function participating in name lookup. In this case  
the behaviour also changes because the new function is a better match.  
Note that the signature of the exported template remains the same.

Note: I think the same should apply even if 'g' was not a template, but  
GCC disagrees (tested without 'export' of course). VC does what I expect  
though. I wish I knew which is right.


-- 
FE


Post a reply to this message

From: Darren New
Subject: Re: A question about Java generics (not a flame)
Date: 14 May 2008 16:52:33
Message: <482b5111$1@news.povray.org>
Fredrik Eriksson wrote:
> On Wed, 14 May 2008 18:23:54 +0200, Darren New <dne### [at] sanrrcom> wrote:
>>
>> *** Main2.cpp
>>
>> #include <A.h>
>>
>> void A::xx(int i) {
>>    cout << "Beta!\n";
>> }
> 
> This will fail to compile, because you cannot redefine A::xx.

I didn't. I created an overloaded version. One takes an int, the other 
takes a long.  Am I *completely* clueless about C++?

My intent was to get one match to do a silent upcast from int to long, 
and the other to match the more-appropriate int-accepting function.

-- 
   Darren New / San Diego, CA, USA (PST)
     "That's pretty. Where's that?"
          "It's the Age of Channelwood."
     "We should go there on vacation some time."


Post a reply to this message

From: Fredrik Eriksson
Subject: Re: A question about Java generics (not a flame)
Date: 14 May 2008 17:01:28
Message: <op.ua5woqdy7bxctx@e6600>
On Wed, 14 May 2008 22:52:33 +0200, Darren New <dne### [at] sanrrcom> wrote:
> Fredrik Eriksson wrote:
>> On Wed, 14 May 2008 18:23:54 +0200, Darren New <dne### [at] sanrrcom> wrote:
>>>
>>> *** Main2.cpp
>>>
>>> #include <A.h>
>>>
>>> void A::xx(int i) {
>>>    cout << "Beta!\n";
>>> }
>>  This will fail to compile, because you cannot redefine A::xx.
>
> I didn't. I created an overloaded version. One takes an int, the other  
> takes a long.  Am I *completely* clueless about C++?

You cannot add overloads to class methods from outside the class. All  
overloads must be declared in the class declaration.


-- 
FE


Post a reply to this message

From: Darren New
Subject: Re: A question about Java generics (not a flame)
Date: 14 May 2008 17:08:28
Message: <482b54cc$1@news.povray.org>
Fredrik Eriksson wrote:
> You cannot add overloads to class methods from outside the class. All 
> overloads must be declared in the class declaration.

Thanks!

-- 
   Darren New / San Diego, CA, USA (PST)
     "That's pretty. Where's that?"
          "It's the Age of Channelwood."
     "We should go there on vacation some time."


Post a reply to this message

From: Warp
Subject: Re: A question about Java generics (not a flame)
Date: 15 May 2008 08:45:40
Message: <482c3074@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> *** A.h

> void X<T>(void);

> class A {
>    void xx(long i) {
>      cout << "Alpha!\n";
>    }
> }

> #include <A.h>

> void A::xx(int i) {
>    cout << "Beta!\n";
> }

  You are trying to implement a function void A::xx(int) which hasn't
been declared in the class A declaration. It won't compile.

  Note that void A::xx(long) and void A::xx(int) are two different functions,
with different fingerprints. You cannot override one with the other.

-- 
                                                          - Warp


Post a reply to this message

From: Warp
Subject: Re: A question about Java generics (not a flame)
Date: 15 May 2008 08:49:55
Message: <482c3173@news.povray.org>
Fredrik Eriksson <fe79}--at--{yahoo}--dot--{com> wrote:
> Now make a small change to main.cpp:

> // main.cpp
>    export template <typename T> void f();

>    #include "g.hpp"
>    template <> void g(int i) { /* Do something completely different */ }

>    int main(){ f<int>(); }
> // end main.cpp

  I don't see how this causes a problem described earlier. This simply
requires main.cpp to be recompiled (after all, it's the one which has
been changed), which will result in a new object file which does not
use the export template.

  Why would this cause the need to recompile the export template for
each usage in the program?

-- 
                                                          - Warp


Post a reply to this message

From: Darren New
Subject: Re: A question about Java generics (not a flame)
Date: 15 May 2008 11:59:34
Message: <482c5de6$1@news.povray.org>
Warp wrote:
>   You are trying to implement a function void A::xx(int) which hasn't
> been declared in the class A declaration. It won't compile.

That I didn't know. Will it work if you just make xx(int) and xx(long) 
globals?

>   Note that void A::xx(long) and void A::xx(int) are two different functions,
> with different fingerprints. You cannot override one with the other.

Well, I was trying to overload, not override as such.

-- 
   Darren New / San Diego, CA, USA (PST)
     "That's pretty. Where's that?"
          "It's the Age of Channelwood."
     "We should go there on vacation some time."


Post a reply to this message

From: Warp
Subject: Re: A question about Java generics (not a flame)
Date: 15 May 2008 12:07:59
Message: <482c5fdf@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> That I didn't know. Will it work if you just make xx(int) and xx(long) 
> globals?

  Maybe what you are trying to write is something like this:

foo.cc:
export template<typename T>
void foo(T t) { bar(t); }


a.cc:
export template<typename T>
void foo(T t);

void bar(int i);

void a() { foo(5); }


b.cc:
export template<typename T>
void foo(T t);

void bar(long i);

void b() { foo(5); }


  I see how that could make some difference.

-- 
                                                          - Warp


Post a reply to this message

<<< Previous 10 Messages Goto Latest 10 Messages Next 10 Messages >>>

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.