Warning C4244 when using wtime_input_facet with MSVC
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 75/100
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:
- perform an explicit, well-defined wide-to-narrow conversion, or
- 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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from boostorg/date_time
-
Difficulty 1/5 Under an hour Newbie friendliness 68/100
-
Difficulty 3/5 1-2 days Newbie friendliness 68/100
-
Difficulty 3/5 1-2 days Newbie friendliness 45/100
-
Difficulty 3/5 1-2 days Newbie friendliness 45/100
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
All issues in boostorg/date_time
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
AXERA-TECH/ax-llm#77 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
games-on-whales/wolf#509 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
-
bug-unconfirmed
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
NVIDIA/cuda-samples#453 ·