Prevent accidential misuse of TUNE()

Writing a TUNE expression like

int smallNetThreshold = 962;
TUNE(smallNetThreshold, 850, 1050);

is wrong and can lead to a weird binary. It should thus fail to even compile,
with the proper intellisense you'll also see the error directly in your editor.

A cheap way to prevent this is to disallow any fundamental type in the parameter list.

closes https://github.com/official-stockfish/Stockfish/pull/6164

No functional change
This commit is contained in:
Disservin
2025-07-24 10:28:17 +02:00
committed by Joost VandeVondele
parent bdc393d3a2
commit e88ccfd835
+10 -1
View File
@@ -170,11 +170,20 @@ class Tune {
static OptionsMap* options;
};
template<typename... Args>
constexpr void tune_check_args(Args&&...) {
static_assert((!std::is_fundamental_v<Args> && ...), "TUNE macro arguments wrong");
}
// Some macro magic :-) we define a dummy int variable that the compiler initializes calling Tune::add()
#define STRINGIFY(x) #x
#define UNIQUE2(x, y) x##y
#define UNIQUE(x, y) UNIQUE2(x, y) // Two indirection levels to expand __LINE__
#define TUNE(...) int UNIQUE(p, __LINE__) = Tune::add(STRINGIFY((__VA_ARGS__)), __VA_ARGS__)
#define TUNE(...) \
int UNIQUE(p, __LINE__) = []() -> int { \
tune_check_args(__VA_ARGS__); \
return Tune::add(STRINGIFY((__VA_ARGS__)), __VA_ARGS__); \
}();
#define UPDATE_ON_LAST() bool UNIQUE(p, __LINE__) = Tune::update_on_last = true