Avoid "using namespace std"

This is a cleanup PR that prepares the automatic checking of missing or
superfluous #include directives via the include-what-you-use (IWYU) tool
on the CI. Unfortunately, IWYU proposes additional includes for
"namespace std" although we don't need them.

To avoid the problem, the commit removes all "using namespace std"
statements from the code and directly uses the std:: prefix instead.
Alternatively, we could add specific usings (e.g. "using std::string")
foreach used type. Also, a mix of both approaches would be possible.
I decided for the prefix approach because most of the files were already
using the std:: prefixes despite the "using namespace std".

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

No functional change
This commit is contained in:
Sebastian Buchwald
2023-09-04 22:01:20 +02:00
committed by Disservin
parent b25d68f6ee
commit a8b4fd1671
4 changed files with 80 additions and 88 deletions

View File

@@ -41,8 +41,6 @@
#include "search.h"
#include "thread.h"
using namespace std;
namespace Stockfish {
namespace {
@@ -56,10 +54,10 @@ namespace {
// the initial position ("startpos") and then makes the moves given in the following
// move list ("moves").
void position(Position& pos, istringstream& is, StateListPtr& states) {
void position(Position& pos, std::istringstream& is, StateListPtr& states) {
Move m;
string token, fen;
std::string token, fen;
is >> token;
@@ -103,11 +101,11 @@ namespace {
// setoption() is called when the engine receives the "setoption" UCI command.
// The function updates the UCI option ("name") to the given value ("value").
void setoption(istringstream& is) {
void setoption(std::istringstream& is) {
Threads.main()->wait_for_search_finished();
string token, name, value;
std::string token, name, value;
is >> token; // Consume the "name" token
@@ -130,10 +128,10 @@ namespace {
// sets the thinking time and other parameters from the input string, then starts
// with a search.
void go(Position& pos, istringstream& is, StateListPtr& states) {
void go(Position& pos, std::istringstream& is, StateListPtr& states) {
Search::LimitsType limits;
string token;
std::string token;
bool ponderMode = false;
limits.startTime = now(); // The search starts as early as possible
@@ -164,24 +162,24 @@ namespace {
// Firstly, a list of UCI commands is set up according to the bench
// parameters, then it is run one by one, printing a summary at the end.
void bench(Position& pos, istream& args, StateListPtr& states) {
void bench(Position& pos, std::istream& args, StateListPtr& states) {
string token;
std::string token;
uint64_t num, nodes = 0, cnt = 1;
vector<string> list = setup_bench(pos, args);
num = count_if(list.begin(), list.end(), [](const string& s) { return s.find("go ") == 0 || s.find("eval") == 0; });
std::vector<std::string> list = setup_bench(pos, args);
num = count_if(list.begin(), list.end(), [](const std::string& s) { return s.find("go ") == 0 || s.find("eval") == 0; });
TimePoint elapsed = now();
for (const auto& cmd : list)
{
istringstream is(cmd);
is >> skipws >> token;
std::istringstream is(cmd);
is >> std::skipws >> token;
if (token == "go" || token == "eval")
{
cerr << "\nPosition: " << cnt++ << '/' << num << " (" << pos.fen() << ")" << endl;
std::cerr << "\nPosition: " << cnt++ << '/' << num << " (" << pos.fen() << ")" << std::endl;
if (token == "go")
{
go(pos, is, states);
@@ -200,10 +198,10 @@ namespace {
dbg_print();
cerr << "\n==========================="
<< "\nTotal time (ms) : " << elapsed
<< "\nNodes searched : " << nodes
<< "\nNodes/second : " << 1000 * nodes / elapsed << endl;
std::cerr << "\n==========================="
<< "\nTotal time (ms) : " << elapsed
<< "\nNodes searched : " << nodes
<< "\nNodes/second : " << 1000 * nodes / elapsed << std::endl;
}
// The win rate model returns the probability of winning (in per mille units) given an
@@ -244,7 +242,7 @@ namespace {
void UCI::loop(int argc, char* argv[]) {
Position pos;
string token, cmd;
std::string token, cmd;
StateListPtr states(new std::deque<StateInfo>(1));
pos.set(StartFEN, false, &states->back(), Threads.main());
@@ -253,13 +251,13 @@ void UCI::loop(int argc, char* argv[]) {
cmd += std::string(argv[i]) + " ";
do {
if (argc == 1 && !getline(cin, cmd)) // Wait for an input or an end-of-file (EOF) indication
if (argc == 1 && !getline(std::cin, cmd)) // Wait for an input or an end-of-file (EOF) indication
cmd = "quit";
istringstream is(cmd);
std::istringstream is(cmd);
token.clear(); // Avoid a stale if getline() returns nothing or a blank line
is >> skipws >> token;
is >> std::skipws >> token;
if ( token == "quit"
|| token == "stop")
@@ -294,7 +292,7 @@ void UCI::loop(int argc, char* argv[]) {
{
std::optional<std::string> filename;
std::string f;
if (is >> skipws >> f)
if (is >> std::skipws >> f)
filename = f;
Eval::NNUE::save_eval(filename);
}
@@ -325,11 +323,11 @@ int UCI::to_cp(Value v) {
/// mate <y> Mate in 'y' moves (not plies). If the engine is getting mated,
/// uses negative values for 'y'.
string UCI::value(Value v) {
std::string UCI::value(Value v) {
assert(-VALUE_INFINITE < v && v < VALUE_INFINITE);
stringstream ss;
std::stringstream ss;
if (abs(v) < VALUE_TB_WIN_IN_MAX_PLY)
ss << "cp " << UCI::to_cp(v);
@@ -348,9 +346,9 @@ string UCI::value(Value v) {
/// UCI::wdl() reports the win-draw-loss (WDL) statistics given an evaluation
/// and a game ply based on the data gathered for fishtest LTC games.
string UCI::wdl(Value v, int ply) {
std::string UCI::wdl(Value v, int ply) {
stringstream ss;
std::stringstream ss;
int wdl_w = win_rate_model( v, ply);
int wdl_l = win_rate_model(-v, ply);
@@ -373,7 +371,7 @@ std::string UCI::square(Square s) {
/// standard chess mode and in e1h1 notation it is printed in Chess960 mode.
/// Internally, all castling moves are always encoded as 'king captures rook'.
string UCI::move(Move m, bool chess960) {
std::string UCI::move(Move m, bool chess960) {
if (m == MOVE_NONE)
return "(none)";
@@ -387,7 +385,7 @@ string UCI::move(Move m, bool chess960) {
if (type_of(m) == CASTLING && !chess960)
to = make_square(to > from ? FILE_G : FILE_C, rank_of(from));
string move = UCI::square(from) + UCI::square(to);
std::string move = UCI::square(from) + UCI::square(to);
if (type_of(m) == PROMOTION)
move += " pnbrqk"[promotion_type(m)];
@@ -399,7 +397,7 @@ string UCI::move(Move m, bool chess960) {
/// UCI::to_move() converts a string representing a move in coordinate notation
/// (g1f3, a7a8q) to the corresponding legal Move, if any.
Move UCI::to_move(const Position& pos, string& str) {
Move UCI::to_move(const Position& pos, std::string& str) {
if (str.length() == 5)
str[4] = char(tolower(str[4])); // The promotion piece character must be lowercased