Skip to content

Warning C4244 when using wtime_input_facet with MSVC #253

Description

@jalegido

Description

When using boost::posix_time::wtime_input_facet with a wide-character stream, MSVC 2022 reports warning C4244 because time_input_facet::check_special_value converts a std::wstring to std::string through convert_string_type<wchar_t, char>.

The conversion appears to construct a std::string directly from const wchar_t* iterators, which causes a narrowing conversion and may result in data loss.

Minimal example

#include <boost/date_time/posix_time/posix_time.hpp>

#include <locale>
#include <sstream>

int main()
{
    std::wistringstream input(L"not-a-valid-ptime");

    input.imbue(std::locale(
        input.getloc(),
        new boost::posix_time::wtime_input_facet));

    boost::posix_time::ptime value;
    input >> value;

    return 0;
}

Environment

  • Visual Studio 2022 Professional
  • MSVC 14.44.35207
  • Boost installed through vcpkg
  • Target: x64-windows
  • Warning level: /W4

Actual result

The project builds successfully, but MSVC reports:

C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.44.35207\include\xutility(4813,18):
warning C4244: '=': conversion from 'const wchar_t' to 'char', possible loss of data

The template instantiation trace points to:

boost/date_time/time_facet.hpp(1117,31):
time_input_facet<ptime, wchar_t>::check_special_value

boost/date_time/time_facet.hpp(1260,29):
convert_string_type<wchar_t, char>

boost/date_time/string_convert.hpp(26,12):
std::basic_string<char>::insert(...)

The relevant code path is equivalent to:

std::string tmp = convert_string_type<char_type, char>(mr.cache);

where char_type is wchar_t.

Expected result

Using wtime_input_facet should not produce a narrowing-conversion warning when compiling with /W4.

The conversion used to create the diagnostic message should either:

  1. perform an explicit, well-defined wide-to-narrow conversion, or
  2. preserve the original character type until the diagnostic is formatted.

Could this be fixed or suppressed safely for the wchar_t instantiation?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions