boostorg / boostorg/date_time

Warning C4244 when using wtime_input_facet with MSVC

Open
#253 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
70
Forks
99
PR merge metrics
No merged PRs in 30d

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`.

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

```cpp
#include

#include
#include

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:

```text
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:

```text
boost/date_time/time_facet.hpp(1117,31):
time_input_facet::check_special_value

boost/date_time/time_facet.hpp(1260,29):
convert_string_type

boost/date_time/string_convert.hpp(26,12):
std::basic_string::insert(...)
```

The relevant code path is equivalent to:

```cpp
std::string tmp = convert_string_type(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?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.