From 3da01e3753bc3be7e90fe7dabd62cf0781dc2289 Mon Sep 17 00:00:00 2001
From: Matthias Moulin <matthias.moulin@gmail.com>
Date: Sun, 7 Apr 2024 13:50:00 +0200
Subject: [PATCH] Worked around error C2572: 'std::enable_if': redefinition of
 default argument: parameter 1

All std C++ headers are hidden and replaced with import std.
Some std C headers are added.

Cfr. https://developercommunity.visualstudio.com/t/VS2022-175-Preview-3---Compiler-bug-wit/10256508
---
 include/fmt/args.h       |  8 +++--
 include/fmt/base.h       | 14 +++++---
 include/fmt/chrono.h     | 20 ++++++-----
 include/fmt/compile.h    |  4 ++-
 include/fmt/format-inl.h | 16 +++++----
 include/fmt/format.h     | 32 +++++++++++------
 include/fmt/os.h         | 10 +++---
 include/fmt/ostream.h    |  4 ++-
 include/fmt/printf.h     |  6 ++--
 include/fmt/ranges.h     | 10 +++---
 include/fmt/std.h        | 48 ++++++++++++++++---------
 include/fmt/xchar.h      |  8 +++--
 src/fmt.cc               | 77 +++++++++++++++++++++++-----------------
 src/os.cc                |  4 ++-
 14 files changed, 163 insertions(+), 98 deletions(-)

diff --git a/include/fmt/args.h b/include/fmt/args.h
index b77a2d066112..0a93c1753f38 100644
--- a/include/fmt/args.h
+++ b/include/fmt/args.h
@@ -8,9 +8,11 @@
 #ifndef FMT_ARGS_H_
 #define FMT_ARGS_H_
 
-#include <functional>  // std::reference_wrapper
-#include <memory>      // std::unique_ptr
-#include <vector>
+// DIVERGENCE BEGIN - Worked around std imports and includes mixing
+//#include <functional>  // std::reference_wrapper
+//#include <memory>      // std::unique_ptr
+//#include <vector>
+// DIVERGENCE END
 
 #include "format.h"  // std_string_view
 
diff --git a/include/fmt/base.h b/include/fmt/base.h
index 91015edfae64..a249673f2ecd 100644
--- a/include/fmt/base.h
+++ b/include/fmt/base.h
@@ -8,13 +8,17 @@
 #ifndef FMT_BASE_H_
 #define FMT_BASE_H_
 
-#include <limits.h>  // CHAR_BIT
-#include <stdio.h>   // FILE
-#include <string.h>  // strlen
+// DIVERGENCE BEGIN - Worked around std imports and includes mixing
+//#include <limits.h>  // CHAR_BIT
+//#include <stdio.h>   // FILE
+//#include <string.h>  // strlen
+// DIVERGENCE END
 
 // <cstddef> is also included transitively from <type_traits>.
-#include <cstddef>      // std::byte
-#include <type_traits>  // std::enable_if
+// DIVERGENCE BEGIN - Worked around std imports and includes mixing
+//#include <cstddef>      // std::byte
+//#include <type_traits>  // std::enable_if
+// DIVERGENCE END
 
 // The fmt library version in the form major * 10000 + minor * 100 + patch.
 #define FMT_VERSION 100202
diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h
index d2e10b56b933..d6589f14f3b4 100644
--- a/include/fmt/chrono.h
+++ b/include/fmt/chrono.h
@@ -8,15 +8,17 @@
 #ifndef FMT_CHRONO_H_
 #define FMT_CHRONO_H_
 
-#include <algorithm>
-#include <chrono>
-#include <cmath>    // std::isfinite
-#include <cstring>  // std::memcpy
-#include <ctime>
-#include <iterator>
-#include <locale>
-#include <ostream>
-#include <type_traits>
+// DIVERGENCE BEGIN - Worked around std imports and includes mixing
+//#include <algorithm>
+//#include <chrono>
+//#include <cmath>    // std::isfinite
+//#include <cstring>  // std::memcpy
+//#include <ctime>
+//#include <iterator>
+//#include <locale>
+//#include <ostream>
+//#include <type_traits>
+// DIVERGENCE END
 
 #include "format.h"
 
diff --git a/include/fmt/compile.h b/include/fmt/compile.h
index d6278521fd0c..01e3e83c7b82 100644
--- a/include/fmt/compile.h
+++ b/include/fmt/compile.h
@@ -8,7 +8,9 @@
 #ifndef FMT_COMPILE_H_
 #define FMT_COMPILE_H_
 
-#include <iterator>  // std::back_inserter
+// DIVERGENCE BEGIN - Worked around std imports and includes mixing
+//#include <iterator>  // std::back_inserter
+// DIVERGENCE END
 
 #include "format.h"
 
diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h
index dfd96b49b54e..86d8540cfa99 100644
--- a/include/fmt/format-inl.h
+++ b/include/fmt/format-inl.h
@@ -8,14 +8,18 @@
 #ifndef FMT_FORMAT_INL_H_
 #define FMT_FORMAT_INL_H_
 
-#include <algorithm>
-#include <cerrno>  // errno
-#include <climits>
-#include <cmath>
-#include <exception>
+// DIVERGENCE BEGIN - Worked around std imports and includes mixing
+//#include <algorithm>
+//#include <cerrno>  // errno
+//#include <climits>
+//#include <cmath>
+//#include <exception>
+// DIVERGENCE END
 
 #ifndef FMT_STATIC_THOUSANDS_SEPARATOR
-#  include <locale>
+// DIVERGENCE BEGIN - Worked around std imports and includes mixing
+//#  include <locale>
+// DIVERGENCE END
 #endif
 
 #if defined(_WIN32) && !defined(FMT_USE_WRITE_CONSOLE)
diff --git a/include/fmt/format.h b/include/fmt/format.h
index d146ec34ee4b..d6f1d31ac6fd 100644
--- a/include/fmt/format.h
+++ b/include/fmt/format.h
@@ -38,30 +38,40 @@
 #  define FMT_REMOVE_TRANSITIVE_INCLUDES
 #endif
 
-#include <cmath>             // std::signbit
-#include <cstdint>           // uint32_t
-#include <cstring>           // std::memcpy
-#include <initializer_list>  // std::initializer_list
-#include <limits>            // std::numeric_limits
+// DIVERGENCE BEGIN - Worked around std imports and includes mixing
+//#include <cmath>             // std::signbit
+//#include <cstdint>           // uint32_t
+//#include <cstring>           // std::memcpy
+//#include <initializer_list>  // std::initializer_list
+//#include <limits>            // std::numeric_limits
+// DIVERGENCE END
 #if defined(__GLIBCXX__) && !defined(_GLIBCXX_USE_DUAL_ABI)
 // Workaround for pre gcc 5 libstdc++.
-#  include <memory>  // std::allocator_traits
+// DIVERGENCE BEGIN - Worked around std imports and includes mixing
+//#  include <memory>  // std::allocator_traits
+// DIVERGENCE END
 #endif
-#include <stdexcept>     // std::runtime_error
-#include <string>        // std::string
-#include <system_error>  // std::system_error
+// DIVERGENCE BEGIN - Worked around std imports and includes mixing
+//#include <stdexcept>     // std::runtime_error
+//#include <string>        // std::string
+//#include <system_error>  // std::system_error
+// DIVERGENCE END
 
 #include "base.h"
 
 // Checking FMT_CPLUSPLUS for warning suppression in MSVC.
 #if FMT_HAS_INCLUDE(<bit>) && FMT_CPLUSPLUS > 201703L
-#  include <bit>  // std::bit_cast
+// DIVERGENCE BEGIN - Worked around std imports and includes mixing
+//#  include <bit>  // std::bit_cast
+// DIVERGENCE END
 #endif
 
 // libc++ supports string_view in pre-c++17.
 #if FMT_HAS_INCLUDE(<string_view>) && \
     (FMT_CPLUSPLUS >= 201703L || defined(_LIBCPP_VERSION))
-#  include <string_view>
+// DIVERGENCE BEGIN - Worked around std imports and includes mixing
+//#  include <string_view>
+// DIVERGENCE END
 #  define FMT_USE_STRING_VIEW
 #endif
 
diff --git a/include/fmt/os.h b/include/fmt/os.h
index 3a10c9303f11..b049c43f24b9 100644
--- a/include/fmt/os.h
+++ b/include/fmt/os.h
@@ -8,10 +8,12 @@
 #ifndef FMT_OS_H_
 #define FMT_OS_H_
 
-#include <cerrno>
-#include <cstddef>
-#include <cstdio>
-#include <system_error>  // std::system_error
+// DIVERGENCE BEGIN - Worked around std imports and includes mixing
+//#include <cerrno>
+//#include <cstddef>
+//#include <cstdio>
+//#include <system_error>  // std::system_error
+// DIVERGENCE END
 
 #include "format.h"
 
diff --git a/include/fmt/ostream.h b/include/fmt/ostream.h
index f9ba9d7b25b6..89fbbba954af 100644
--- a/include/fmt/ostream.h
+++ b/include/fmt/ostream.h
@@ -8,7 +8,9 @@
 #ifndef FMT_OSTREAM_H_
 #define FMT_OSTREAM_H_
 
-#include <fstream>  // std::filebuf
+// DIVERGENCE BEGIN - Worked around std imports and includes mixing
+//#include <fstream>  // std::filebuf
+// DIVERGENCE END
 
 #ifdef _WIN32
 #  ifdef __GLIBCXX__
diff --git a/include/fmt/printf.h b/include/fmt/printf.h
index c060952d9e8b..81fb0ae34215 100644
--- a/include/fmt/printf.h
+++ b/include/fmt/printf.h
@@ -8,8 +8,10 @@
 #ifndef FMT_PRINTF_H_
 #define FMT_PRINTF_H_
 
-#include <algorithm>  // std::max
-#include <limits>     // std::numeric_limits
+// DIVERGENCE BEGIN - Worked around std imports and includes mixing
+//#include <algorithm>  // std::max
+//#include <limits>     // std::numeric_limits
+// DIVERGENCE END
 
 #include "format.h"
 
diff --git a/include/fmt/ranges.h b/include/fmt/ranges.h
index f2b0f0e944dc..3b4fc14940ff 100644
--- a/include/fmt/ranges.h
+++ b/include/fmt/ranges.h
@@ -8,10 +8,12 @@
 #ifndef FMT_RANGES_H_
 #define FMT_RANGES_H_
 
-#include <initializer_list>
-#include <iterator>
-#include <tuple>
-#include <type_traits>
+// DIVERGENCE BEGIN - Worked around std imports and includes mixing
+//#include <initializer_list>
+//#include <iterator>
+//#include <tuple>
+//#include <type_traits>
+// DIVERGENCE END
 
 #include "format.h"
 
diff --git a/include/fmt/std.h b/include/fmt/std.h
index 7085cae8170a..91e707e814e8 100644
--- a/include/fmt/std.h
+++ b/include/fmt/std.h
@@ -8,43 +8,57 @@
 #ifndef FMT_STD_H_
 #define FMT_STD_H_
 
-#include <atomic>
-#include <bitset>
-#include <complex>
-#include <cstdlib>
-#include <exception>
-#include <memory>
-#include <thread>
-#include <type_traits>
-#include <typeinfo>
-#include <utility>
-#include <vector>
+// DIVERGENCE BEGIN - Worked around std imports and includes mixing
+//#include <atomic>
+//#include <bitset>
+//#include <complex>
+//#include <cstdlib>
+//#include <exception>
+//#include <memory>
+//#include <thread>
+//#include <type_traits>
+//#include <typeinfo>
+//#include <utility>
+//#include <vector>
+// DIVERGENCE END
 
 #include "format.h"
 #include "ostream.h"
 
 #if FMT_HAS_INCLUDE(<version>)
-#  include <version>
+// DIVERGENCE BEGIN - Worked around std imports and includes mixing
+//#  include <version>
+// DIVERGENCE END
 #endif
 // Checking FMT_CPLUSPLUS for warning suppression in MSVC.
 #if FMT_CPLUSPLUS >= 201703L
 #  if FMT_HAS_INCLUDE(<filesystem>)
-#    include <filesystem>
+// DIVERGENCE BEGIN - Worked around std imports and includes mixing
+//#    include <filesystem>
+// DIVERGENCE END
 #  endif
 #  if FMT_HAS_INCLUDE(<variant>)
-#    include <variant>
+// DIVERGENCE BEGIN - Worked around std imports and includes mixing
+//#    include <variant>
+// DIVERGENCE END
 #  endif
 #  if FMT_HAS_INCLUDE(<optional>)
-#    include <optional>
+// DIVERGENCE BEGIN - Worked around std imports and includes mixing
+//#    include <optional>
+// DIVERGENCE END
 #  endif
 #endif
 
 #if FMT_HAS_INCLUDE(<expected>) && FMT_CPLUSPLUS > 202002L
-#  include <expected>
+// DIVERGENCE BEGIN - Worked around std imports and includes mixing
+//#  include <expected>
+// DIVERGENCE END
 #endif
 
 #if FMT_CPLUSPLUS > 201703L && FMT_HAS_INCLUDE(<source_location>)
-#  include <source_location>
+// DIVERGENCE BEGIN - Worked around std imports and includes mixing
+//#  include <source_location>
+// DIVERGENCE END
 #endif
 
 // GCC 4 does not support FMT_HAS_INCLUDE.
diff --git a/include/fmt/xchar.h b/include/fmt/xchar.h
index 61a4b54fca5d..98e08f11fd1b 100644
--- a/include/fmt/xchar.h
+++ b/include/fmt/xchar.h
@@ -8,14 +8,18 @@
 #ifndef FMT_XCHAR_H_
 #define FMT_XCHAR_H_
 
-#include <cwchar>
+// DIVERGENCE BEGIN - Worked around std imports and includes mixing
+//#include <cwchar>
+// DIVERGENCE END
 
 #include "color.h"
 #include "format.h"
 #include "ranges.h"
 
 #ifndef FMT_STATIC_THOUSANDS_SEPARATOR
-#  include <locale>
+// DIVERGENCE BEGIN - Worked around std imports and includes mixing
+//#  include <locale>
+// DIVERGENCE END
 #endif
 
 FMT_BEGIN_NAMESPACE
diff --git a/src/fmt.cc b/src/fmt.cc
index 6cff309d41c6..05c9189af3af 100644
--- a/src/fmt.cc
+++ b/src/fmt.cc
@@ -2,38 +2,47 @@ module;
 
 // Put all implementation-provided headers into the global module fragment
 // to prevent attachment to this module.
-#include <algorithm>
-#include <cerrno>
-#include <chrono>
-#include <climits>
-#include <cmath>
-#include <cstddef>
-#include <cstdint>
-#include <cstdio>
-#include <cstdlib>
-#include <cstring>
-#include <ctime>
-#include <exception>
-#include <filesystem>
-#include <fstream>
-#include <functional>
-#include <iterator>
-#include <limits>
-#include <locale>
-#include <memory>
-#include <optional>
-#include <ostream>
-#include <stdexcept>
-#include <string>
-#include <string_view>
-#include <system_error>
-#include <thread>
-#include <type_traits>
-#include <typeinfo>
-#include <utility>
-#include <variant>
-#include <vector>
-#include <version>
+// DIVERGENCE BEGIN - Worked around std imports and includes mixing
+//#include <algorithm>
+//#include <cerrno>
+//#include <chrono>
+//#include <climits>
+//#include <cmath>
+//#include <cstddef>
+//#include <cstdint>
+//#include <cstdio>
+//#include <cstdlib>
+//#include <cstring>
+//#include <ctime>
+//#include <exception>
+//#include <filesystem>
+//#include <fstream>
+//#include <functional>
+//#include <iterator>
+//#include <limits>
+//#include <locale>
+//#include <memory>
+//#include <optional>
+//#include <ostream>
+//#include <stdexcept>
+//#include <string>
+//#include <string_view>
+//#include <system_error>
+//#include <thread>
+//#include <type_traits>
+//#include <typeinfo>
+//#include <utility>
+//#include <variant>
+//#include <vector>
+//#include <version>
+// DIVERGENCE END
+
+// DIVERGENCE BEGIN - Worked around std imports and includes mixing
+#include <limits.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <time.h>
+// DIVERGENCE END
 
 #if __has_include(<cxxabi.h>)
 #  include <cxxabi.h>
@@ -70,6 +79,10 @@ module;
 
 export module fmt;
 
+// DIVERGENCE BEGIN - Worked around std imports and includes mixing
+import std;
+// DIVERGENCE END
+
 #define FMT_EXPORT export
 #define FMT_BEGIN_EXPORT export {
 #define FMT_END_EXPORT }
diff --git a/src/os.cc b/src/os.cc
index 30bb0a6f1b00..a2926e6d0f22 100644
--- a/src/os.cc
+++ b/src/os.cc
@@ -12,7 +12,9 @@
 
 #include "fmt/os.h"
 
-#include <climits>
+// DIVERGENCE BEGIN - Worked around std imports and includes mixing
+//#include <climits>
+// DIVERGENCE END
 
 #if FMT_USE_FCNTL
 #  include <sys/stat.h>