如何在不声明 C/C++ 中附加函数的情况下删除重复的代码?
How can I de-duplicate code without declaring an additional function in C/C++?
我正在开发一个国际象棋引擎,并正在研究移动生成。例如,这是我为黑骑士生成动作的函数:
/** Pseudolegal moves don't take check into account. */
std::vector<uint8_t>
generate_pseudolegal_bknight_moves(std::shared_ptr<Position> position,
uint8_t square) {
assert(VALID_SQUARE(square));
assert(position->mailbox[square] == B_KNIGHT);
uint8_t candidates[8] = {
NEXT_RANK(PREV_FILE(PREV_FILE(square))),
NEXT_RANK(NEXT_RANK(PREV_FILE(square))),
PREV_RANK(PREV_FILE(PREV_FILE(square))),
PREV_RANK(PREV_RANK(PREV_FILE(square))),
NEXT_RANK(NEXT_FILE(NEXT_FILE(square))),
NEXT_RANK(NEXT_RANK(NEXT_FILE(square))),
PREV_RANK(NEXT_FILE(NEXT_FILE(square))),
PREV_RANK(PREV_RANK(NEXT_FILE(square))),
};
std::vector<uint8_t> moves;
for (int i = 0; i < 8; i++) {
uint8_t candidate = candidates[i];
uint8_t piece = position->mailbox[candidate];
if (VALID_SQUARE(candidate) && (!IS_BLACK_PIECE(piece))) {
moves.push_back(candidate);
}
}
return moves;
}
生成白骑士招式的功能非常相似,只有两个术语(宏)发生了变化:B_KNIGHT
->W_KNIGHT
,IS_BLACK_PIECE
->IS_WHITE_PIECE
。
我不想基本上为每个部分复制移动生成函数,但到目前为止一直这样做,因为它的运行时开销最小。 我可以在 args 中包含bool is_white
或其他东西,并使用三元is_white ? W_KNIGHT : B_KNIGHT
切换术语,但条件会在运行时增加以前不存在的开销,而且看起来并不那么优雅。我想知道是否有一些编译时功能可以帮助我有一个函数定义。
我想我也可以使用内联函数来尝试减少重复代码的数量,但我想知道是否有比这更好的替代方案。
如果你不想要开销,你可以使用模板参数并if constexpr
:
enum class Color { WHITE, BLACK };
template <Color C> std::vector<uint8_t>
generate_pseudolegal_knight_moves(std::shared_ptr<Position> position,
uint8_t square) {
...
if constexpr (C == Color::WHITE) {
assert(position->mailbox[square] == W_KNIGHT);
} else {
assert(position->mailbox[square] == B_KNIGHT);
}
...
}
// Call
auto moves = generate_pseudolegal_knight_moves<Color::WHITE>(...);
该标准保证在编译时将评估条件,并且将丢弃错误的分支。
相关文章:
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- 为什么需要复制构造函数,在哪些情况下它们非常有用
- 在不传递参数数量且只有3个点的情况下,如何使用变差函数
- 在这种情况下,java对象是否可以调用本机函数
- 这个c++代码是如何在没有定义函数的情况下运行的
- 为什么不调用移动构造函数?(默认情况下只有构造器,没有别的)
- 如何在没有函数的情况下编写此代码并使C++更简单?
- 在这种情况下显式调用时,std::cout 如何更改析构函数的行为?
- A类的定义需要B类的定义,如何在不公开B的定义的情况下公开A的公共函数?
- 如何在不使用指针的情况下将派生类的对象作为参数传递给基类中的函数?
- 有没有办法在不使用 #ifdef 的情况下不编译发布版本中的单元测试函数体?
- C++ strcpy 函数在少数主要情况下失败
- C++有什么方法可以在既不调用函数模板也不提供其模板参数的情况下引用函数模板?
- 是否可以在不填充自己的参数的情况下将模板函数作为参数传递?
- 如何在没有复制构造函数的情况下为地图设置值?
- 在这种情况下,如何传递成员函数而不是函数?
- C++:带有大括号初始化列表的函数调用表达式 - 标准是否规定在单个元素列表的微不足道的情况下忽略大括号?
- 在没有默认构造函数的情况下创建的派生对象
- 在双重继承的情况下如何处理非标准构造函数