Poco :: Path compiles with const wchar_t *, but behaves unexpectedly

Working with Poco::Path I found a very interesting error. See the following code:

 #include <iostream> #include <string> #include <Poco/Path.h> int main() { std::wstring a_path = L"c:\\temp"; //Poco::Path from_wstring(a_path); // ERROR: fails to compile, expected Poco::Path from_wchar_t(a_path.c_str()); // compiles... unexpected std::cout << from_wchar_t.toString() << std::endl; return 0; } 

But the output of the above program (on Windows):

\

instead of expected:

C: \ Temp

Having Poco::Path documentation, I don't see a constructor expecting std::wstring (why the first path failed), and not const wchar_t* , only from std::string and const char* (like UTF-8).

How does this compile with const wchar_t* and why unexpected output (wrong path)?

+5
source share
1 answer

When creating mvce for this question, I figured out the problem. I decided to write it here if it is useful for someone else.

The code snippet shown in the question is part of a huge project, so I had no compilation warning:

warning C4800: 'const wchar_t *': forced value for bool 'true' or 'false' (performance warning)

Then I realized that there is a constructor for Poco::Path::Path(bool absolute) and that the compiler automatically translates from the pointer to bool, creating unexpected behavior. Output \ output corresponds to an empty absolute path, its initial value when using such a constructor.


For those interested in a workaround, I now use the conversion of UTF-16 to UTF-8:

 #include <boost/locale/encoding.hpp> // ... std::wstring a_path = L"c:\\temp"; Poco::Path utf8_path(boost::locale::conv::utf_to_utf<char>(a_path)); 
+4
source

Source: https://habr.com/ru/post/1273344/


All Articles