From 97490b0586e9a66f4fa346e57d73cb2e202c752f Mon Sep 17 00:00:00 2001 From: Zephyron Date: Wed, 16 Jul 2025 15:41:41 +1000 Subject: [PATCH] fix: constrain file.h Open templates with C++20 concepts Add HasValueType concept to prevent compilation errors when using raw pointers like const char* with Open() templates. The templates now only participate in overload resolution for types that have a value_type member (e.g., std::string, std::filesystem::path). Fixes C2039 and C2651 errors on Windows builds. Signed-off-by: Zephyron --- src/common/fs/file.h | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/common/fs/file.h b/src/common/fs/file.h index 2e2396075..0dc24308d 100644 --- a/src/common/fs/file.h +++ b/src/common/fs/file.h @@ -34,14 +34,23 @@ void OpenFileStream(FileStream& file_stream, const std::filesystem::path& path, } #ifdef _WIN32 -template -void OpenFileStream(FileStream& file_stream, const Path& path, std::ios_base::openmode open_mode) { - if constexpr (IsChar) { - file_stream.open(std::filesystem::path{ToU8String(path)}, open_mode); - } else { - file_stream.open(std::filesystem::path{path}, open_mode); +#include + +// Concept to check if a type has a value_type member +template +concept HasValueType = requires { typename T::value_type; }; + +// Only enable this overload if Path has value_type + template + void Open(const Path& path, FileAccessMode mode, FileType type = FileType::BinaryFile, + FileShareFlag flag = FileShareFlag::ShareReadOnly) { + using ValueType = typename Path::value_type; + if constexpr (IsChar) { + Open(ToU8String(path), mode, type, flag); + } else { + Open(std::filesystem::path{path}, mode, type, flag); + } } -} #endif /** @@ -184,7 +193,7 @@ public: FileShareFlag flag = FileShareFlag::ShareReadOnly); #ifdef _WIN32 - template + template void Open(const Path& path, FileAccessMode mode, FileType type = FileType::BinaryFile, FileShareFlag flag = FileShareFlag::ShareReadOnly) { using ValueType = typename Path::value_type;