Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Colourspecific code (C++ and templates/traits)

Author: Daniel Clausen

Date: 09:57:59 11/27/03

Go up one level in this thread


Hi Sven!

On November 27, 2003 at 07:01:31, Sven Reichard wrote:

[snip]

>One thing to keep in mind is that template instantiations are decided at
>compile time. So you can't do anything like
>
>void f(Color c)
>{
>  uint64_t bb = AttackTable::ColorTraits<c>::pawnAT[42];
>}
>
>However, I think that this was clear to you.

Yes, I know that. Although I want something similar. (which maybe also doesn't
work :)

I'd like to do this at the end:

template <Color c> f(void)
{
   uint64_t bb = AttackTable::ColorTraits<c>::pawnAT[42];
}

It seems to me that the compiler should know everything at compile time, or am I
missing something?


>Other than that, I can't see from
>the code you posted what went wrong. Could you post or send the code you tried
>to compile?

Here is a little test program, which works:


===========================================================
#include <iostream>

using namespace std;

enum Colour { White, Black };

class AT
{
    public:
        static int whitePawns[4];
        static int blackPawns[4];

        static void init(void)
        {
            for(int i=0; i<4; i++) whitePawns[i] = i, blackPawns[i] = -i;
        }


        template <Colour c> struct ColourTraits;

        template <> struct ColourTraits<White>
        {
            static int pawns(int i) { return AT::whitePawns[i]; }
        };

        template <> struct ColourTraits<Black>
        {
            static int pawns(int i) { return AT::blackPawns[i]; }
        };

};

int AT::whitePawns[4];
int AT::blackPawns[4];


int main(void)
{
    AT::init();

    cout << AT::ColourTraits<White>::pawns(1) << endl; (****)
    cout << AT::ColourTraits<Black>::pawns(2) << endl; (****)

    // The following unfortunately fails.
    cout << AT::ColourTraits<White>::pawns[1] << endl; (****)
    cout << AT::ColourTraits<Black>::pawns[2] << endl;

    return 0;
}
===========================================================

The "problem" here is the line marked with (****). I don't like the fact that I
have to implement the array-access with a function call. Instead I would like to
 replace those () with []. :)

I guess I could simply inline the function and compile with -O (maybe
-fomit-instructions ;), but somehow I wish I wouldn't have the function in the
first place. :)

Sargon

PS. In the real case, the array is 2-dimensional :)



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.