C++ templates question

Discussion of chess software programming and technical issues.

Moderator: Ras

Jose Carlos
Posts: 151
Joined: Wed Mar 08, 2006 10:09 pm
Location: Murcia (Spain)

Re: C++ templates question

Post by Jose Carlos »

Daniel Shawul wrote:Hello Jose
If you are really into templates the best book i can recommend is "Modern C++ Design by Andrei Alexandrescu". Very thorough discussion of template metaprogramming in general. Usually towards the end of the book,I usually get a head ache and still haven't finished it yet ...
Daniel
Thanks, I'll search for it.
__________________________
José Carlos Martínez Galán
Rein Halbersma
Posts: 749
Joined: Tue May 22, 2007 11:13 am

Re: Some advice needed

Post by Rein Halbersma »

mcostalba wrote:
Sven Schüle wrote:The compiler will replace the function call with the correct array element without any overhead since the array index is known at compile time and the array is static, i.e. not part of any object instance and has therefore a fixed address.
Hi Sven, sorry to contradict you but your solution is not the equivalent of constant substitution at compile time (as is the original) in your case the array is actually instantiated in memory at program startup and a memory access (although at fixed address) is done every time the value is required. Because Josè is new to templates I'd prefer to do not mess the discussion with too complex (and incorrect) stuff.
The new C++11 standard has a new constexpr keyword that -if I'm not mistaken- will allow static array elements to be used as template parameters. At least, that is what I infer from section 5.19 of the draft standard at http://www.open-std.org/jtc1/sc22/wg21/ ... /n3242.pdf where array-to-pointer conversions to non-static data are listed as exceptions (implying that array element access to static data should work).

You could test this with gcc 4.6 with the -std=c++0x flag. No such support for Visual C++ yet.
lucasart
Posts: 3241
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: Some advice needed

Post by lucasart »

Man, that's over 1300 pages, written in standardese! and they say C++ is easier than C...
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Some advice needed

Post by bob »

mcostalba wrote:
Sven Schüle wrote:Please accept my apologies


O c'mon Sven you really don't need to apologize for anything :-)

And by the way you were not far from to be correct: array are not allowed but const integer variables are (perhaps surprising) allowed to be used as template parameters by the standard. For instance this works either:

Code: Select all

// We use a const int instead of an enum
template<Color tColor> struct advanceOne1 {
   static const int value = tColor == WHITE ? 8 : -8;
};

template <int n> struct test_compile_time_parameter {}; 

test_compile_time_parameter<advanceOne1<WHITE>::value> t1;  // OK we can pass advanceOne1::value as template parameter 
This is somewhat a bit of hack because it is not clear why single const variable are allowed and arrays are not. The historical reason is that C++ designers wanted to give a stop to the C style #define and so allowed the const integers to be used as a perfect replacement of a #define, for instance you can define a given const int variable in an header file and include it how many times you want and you'll never get a "variable redefinition" error as should be with any other type of variable....C++ is full of these little details: it can be fun or it can be a nightmare according to your mood :-)
That is not only done in c++ compilers. local const int variables were handled the same way in good C compilers. Compiler just has to be convinced that the variable is not changed anywhere else since it could technically be declared as a non-const variable in another file.