Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: CPP related question on double code (B/W)

Author: Bo Persson

Date: 12:47:41 11/26/03

Go up one level in this thread


On November 24, 2003 at 09:42:08, Daniel Clausen wrote:

>On November 24, 2003 at 09:31:34, Steffen Jakob wrote:
>
>>On November 24, 2003 at 08:52:13, Daniel Clausen wrote:
>>
>>>On November 24, 2003 at 08:39:31, Reinhard Scharnagl wrote:
>>>
>>>[snip]
>>>
>>>>I thought on using templates, but they (as far as I know) are fine to
>>>>implement same algorithms to different types. But what I want to have
>>>>is to implement (nearly equal) algorithms for the same type using two
>>>>different names.
>>>
>>>The formal parameter of a template can either be a type parameter (like int,
>>>float, class XY, etc) or a constant expression. The example below illustrates
>>>the 2nd case:
>>>
>>>
>>>#include <iostream>
>>>
>>>template <int colour> void print(void)
>>>{
>>>   std::cout << colour << std::endl;
>>>}
>>>
>>>int main(void)
>>>{
>>>   print<0>();
>>>   print<1>();
>>>
>>>   return 0;
>>>}
>>
>>I use this approach to write color independent code in Hossa which works fine.
>>You have to be careful though because Visual C++ 6 generates wrong code for
>>integer template parameters. If I remember write (I cannot try it out now
>>because I am sitting in Graz waiting for the start of the 4th round) it would
>>create the output
>>
>>0
>>0
>>
>>in your example.
>
>I just tried it with VC++6 and its output is
>1
>1

You are actually both right, it is just a random choice. The compiler produces
to functions, but just includes the types of the function parameters in the
linker info, not the types of the template parameters. A true design error!

This makes the linker believe that there are two instances of the same function,
so it just picks one of them, whichever it feels like.


Bo Persson


>
>So it's wrong, as you predicted.
>
>The code seems to be correct (and produces the expected result with gcc), so is
>there a way that VC++ handles that correctly too?
>
>Sargon



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.