From e88ccfd835544ffa4ae44ebb1231d50b29143c19 Mon Sep 17 00:00:00 2001 From: Disservin Date: Tue, 15 Jul 2025 22:10:26 +0200 Subject: [PATCH] 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 --- src/tune.h | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/tune.h b/src/tune.h index 4dab6bf45..d3c9ebaa3 100644 --- a/src/tune.h +++ b/src/tune.h @@ -170,11 +170,20 @@ class Tune { static OptionsMap* options; }; +template +constexpr void tune_check_args(Args&&...) { + static_assert((!std::is_fundamental_v && ...), "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