C++ code for board[8][8] representation

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

Rein Halbersma
Posts: 741
Joined: Tue May 22, 2007 11:13 am

Re: C++ code for board[8][8] representation

Post by Rein Halbersma »

Code: Select all

#include <array>
  
constexpr auto mapping = [] {
    std::array<int, 128> ret{};
    ret['K'] = 1;
    ret['Q'] = 2;
    return ret;
}();
Or you can just use an immediately invoked lambda expression ;-)
ydebilloez
Posts: 163
Joined: Tue Jun 27, 2017 11:01 pm
Location: Lubumbashi
Full name: Yves De Billoëz

Re: C++ code for board[8][8] representation

Post by ydebilloez »

Thanks
Rein Halbersma wrote: Mon Mar 08, 2021 10:23 pm

Code: Select all

constexpr auto mapping = [] {
    std::array<int, 128> ret{};
}();
Sesse wrote: Mon Mar 08, 2021 7:37 pm

Code: Select all

std::array<int, 128> ret{0};
combining this gives:

Code: Select all

// .h
class bEvaluation {
protected:
  static std::array<int, 128> bPieceIndex;
// .cpp
auto bEvaluation::bPieceIndex = [] {
  std::array<int, 128> bIndex{0};
  bIndex['K'] = 1;
  // etc
warning: declaration requires a global constructor

But lets say, we went halfway around the problem in some way. A static array does not need a destructor...

I still have some related problems:

Code: Select all

appInstance& App() {
  static appInstance instance;
// declaration requires exit time constructor
  return instance;
}
void classname::functname(param) const
{
  static std::string sName = "Hello";
warning: declaration requires exit time constructor
Yves De Billoëz @ macchess belofte chess
Once owner of a Mephisto I, II, challenger, ... chess computer.
Sesse
Posts: 300
Joined: Mon Apr 30, 2018 11:51 pm

Re: C++ code for board[8][8] representation

Post by Sesse »

You need the constexpr part.
ydebilloez
Posts: 163
Joined: Tue Jun 27, 2017 11:01 pm
Location: Lubumbashi
Full name: Yves De Billoëz

Re: C++ code for board[8][8] representation

Post by ydebilloez »

Using a simple global c array ended up easier in C++11...

Still stuck on static std::string X = "" in a function that gives the same error.

I assure you, I can fix it with a simple static char[255]... But I want to keep it c++11 and at the same time getting rid of the destructor error warning.
Yves De Billoëz @ macchess belofte chess
Once owner of a Mephisto I, II, challenger, ... chess computer.