Stockfish: Involuntary conversions of ExtMove to Move

Created on 13 Aug 2017  路  2Comments  路  Source: official-stockfish/Stockfish

The class ExtMove offers an automatic conversion to Move:

enum Move : int {
  MOVE_NONE,
  MOVE_NULL = 65
};

struct ExtMove {
  Move move;
  int value;

  operator Move() const { return move; }
  void operator=(Move m) { move = m; }
};

inline bool operator<(const ExtMove& f, const ExtMove& s) {
  return f.value < s.value;
}

From a designer viewpoint this says: _For any move, you can use an ExtMove too, compiler please arrange for the necessary detail, except for "operator<"._
It makes somehow sense, but the implementation is incomplete.
IMHO the source of the problem is the unscoped enum Move. An unscoped enum out of the box allows relational operations and automatic promotion to the underyling type, which is more than it should here.
If some unsuspecting developer uses an operation, which is not defined for ExtMove, the compiler will happily convert to Move for him and choose from this rich assortment. E.g. try this:
http://coliru.stacked-crooked.com/a/38fdf0e64e877301
Also, this conversion does not play well together with attemtps to "delete" or make "explicit" certain operations with ExtMove.

IMHO the best way to repair this, is to make Move into a struct or at least an unscoped enum and control all operations of Move tightly (please comment)
Alternatively, this suggestion from the OP will make the code safer too:
https://groups.google.com/d/msg/fishcooking/wh5LD_xlDRA/yJcA-89dCgAJ

inline bool operator==(const X& lhs, const X& rhs){return (lhs.move==rhs.move);}
inline bool operator!=(const X& lhs, const X& rhs){return !operator==(lhs,rhs);}
inline bool operator< (const X& lhs, const X& rhs){return (lhs.value< rhs.value);}
inline bool operator> (const X& lhs, const X& rhs){return  operator< (rhs,lhs);}
inline bool operator<=(const X& lhs, const X& rhs){return !operator> (lhs,rhs);}
inline bool operator>=(const X& lhs, const X& rhs){return !operator< (lhs,rhs);}

Most helpful comment

What about this https://github.com/official-stockfish/Stockfish/pull/1205 ?

P.S: The trick is fully out of my hat ! I figured it out myself this afternoon...I think this deserves an article on some C++ blog somewhere :-)

All 2 comments

What about this https://github.com/official-stockfish/Stockfish/pull/1205 ?

P.S: The trick is fully out of my hat ! I figured it out myself this afternoon...I think this deserves an article on some C++ blog somewhere :-)

Fixed with d482e3a8905ee

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rayoh123 picture rayoh123  路  5Comments

anonymous7002 picture anonymous7002  路  3Comments

Alayan-stk-2 picture Alayan-stk-2  路  5Comments

bftjoe picture bftjoe  路  5Comments

mstembera picture mstembera  路  5Comments