4

Working with Poco::Path I've found a very curious error. See 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 is (in Windows):

\

instead of the expected:

c:\temp

Reviewing the Poco::Path documentation I see no constructor expecting std::wstring (that's why the first path fails) nor const wchar_t*, only from std::string and const char* (both UTF-8).

How is it compiling with const wchar_t* and why the unexpected output (wrong path)?

1 Answer 1

3

When creating the mvce for this question I figured out the problem. I've decided to document it here in case it is helpful for somebody else.

The code snippet shown in the question is part of a huge project so I was missing a compilation warning:

warning C4800: 'const wchar_t *' : forcing value to bool 'true' or 'false' (performance warning)

Then I realized that there is a constructor Poco::Path::Path(bool absolute) and that the compiler was making the cast from pointer to bool automatically, producing then the unexpected behavior. The \ of the output corresponds to an empty absolute path, its initial value when using such constructor.


For those interested in a workaround, I'm now using a UTF-16 to UTF-8 conversion:

#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));
1
  • 1
    For future reference: all POCO interfaces (except, obviously, the conversion ones) expect UTF-8 strings. Any conversions are taken care of internally, if needed.
    – Alex
    Nov 15, 2017 at 1:41

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.