openvinotoolkit / openvino

OpenVINO™ is an open-source toolkit for optimizing and deploying AI inference
https://docs.openvino.ai
Apache License 2.0
6.86k stars 2.19k forks source link

CacheManager: std::ofstream constructor error due to missing .c_str() call on std::wstring #26680

Open max-my opened 16 hours ago

max-my commented 16 hours ago

https://github.com/openvinotoolkit/openvino/blob/bdc01107d989310c6efa7dd61c7fa5743c971f1d/src/inference/src/cache_manager.hpp#L128

I encountered a compilation error while building the OpenVINO project, specifically in the cache_manager.hpp file. The error occurs when attempting to construct a std::ofstream object with a std::wstring argument without first calling .c_str().

Error Message:

src/inference/src/cache_manager.hpp:129:91: error: no matching function for call to ‘std::basic_ofstream<wchar_t>::basic_ofstream(std::wstring, std::_Ios_Openmode)’
  129 |         std::wofstream stream(getBlobFile(id), std::ios_base::binary | std::wofstream::out);

Proposed Fix:

To resolve the error, I suggest modifying the line to convert the std::wstring returned by getBlobFile(id) to a const char* using the .c_str() method before passing it to the std::ofstream constructor.

std::ofstream stream(getBlobFile(id).c_str(), std::ios_base::binary | std::ofstream::out);
...
auto blobFileName = getBlobFile(id).c_str();

Justification:

std::ofstream does not have an overload that accepts std::wstring directly. The .c_str() method is necessary to provide the correct type (const char*) expected by the constructor.

ilya-lavrenov commented 15 hours ago

Hi @max-my Could you please send this patch as PR to OpenVINO repo?

KunjShah95 commented 13 hours ago

The error you're encountering is due to the fact that std::ofstream does not support std::wstring directly. Instead, it expects a const char or std::string. To fix this, you need to convert the std::wstring to a std::string or const char before passing it to the std::ofstream constructor.Here’s how you can modify the code to resolve the compilation error:Convert std::wstring to std::string: This approach involves converting the std::wstring to std::string and then using the std::string with std::ofstream. Use std::wofstream for wide characters: If you need to handle wide characters, you should use std::wofstream instead of std::ofstream. Solution 1: Convert std::wstring to std::string

include

include

// Assuming getBlobFile(id) returns a std::wstring std::wstring getBlobFile(int id);

void someFunction(int id) { // Convert std::wstring to std::string std::wstring wstr = getBlobFile(id); std::string str(wstr.begin(), wstr.end());

// Use the converted std::string with std::ofstream
std::ofstream stream(str.c_str(), std::ios_base::binary | std::ofstream::out);
// ... rest of your code

}

Solution 2: Use std::wofstream for wide charactersIf you need to handle wide characters, you should use std::wofstream:

include

include

// Assuming getBlobFile(id) returns a std::wstring std::wstring getBlobFile(int id);

void someFunction(int id) { // Use std::wofstream for wide characters std::wofstream stream(getBlobFile(id), std::ios_base::binary | std::wofstream::out); // ... rest of your code }

Explanation:Choose the solution that best fits your needs. If you don't need to handle wide characters specifically, the first solution is generally more straightforward. If you do need to handle wide characters, the second solution is more appropriate.

KunjShah95 commented 13 hours ago

maxmy kindly check it

KunjShah95 commented 12 hours ago

kindlly check the above text which i made first it contains the solution if it helps u