Warning C4244 when using wtime_input_facet with MSVC

Open Beginner friendly
#253 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
75/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
cpp
Domain
backend

Research direction

Start with boost/date_time/time_facet.hpp at time_input_facet::check_special_value and follow its call to convert_string_type in boost/date_time/string_convert.hpp. Reproduce the minimal wtime_input_facet example with MSVC 2022 at /W4, then verify that the diagnostic conversion no longer emits C4244 while preserving the reported invalid-time behavior.

Written by the indexing model from the issue text.

Description

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?

Dominant language
C++
Stars
70
Forks
99
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from boostorg/date_time

All issues in boostorg/date_time

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.