首页 > 解决方案 > 带有警告 C4244 的 boost::gregorian::date 构造函数

问题描述

此代码片段在我的测试项目中运行良好。但它在我的工作项目中编译时出现警告

auto current_date = boost::gregorian::day_clock::local_day();
const auto nMinYear = current_date.year(); const auto nMonth = current_date.month(); const auto nDay = current_date.day();
const auto nMinYear1 = nMinYear + 1;
boost::gregorian::date holidayDate1(nMinYear1, nMonth, nDay);

在holidayDate1 构造线上。

警告 C4244 'argument':从 'unsigned int' 转换为 'unsigned short',可能丢失数据

我的测试项目在 boost-1.72 上,工作项目在 1.75 上,两者都在 Visual Studio 2019 上。

我尝试使用 grep_year 来包装 nMinYear1 -- holidayDate1(grep_year(nMinYear1 ))-- 它无法编译。

编辑:

我刚试过强制铸造可以解决它,

boost::gregorian::date holidayDate1((unsigned short)nMinYear, (unsigned short)nMonth, (unsigned short)nDay);

但我不明白为什么会发生警告。

标签: c++boost

解决方案


的类型

nMinYear + 1

将是unsigned int由于该表达式中术语的隐含扩展。nMinYear1a 也是如此,当在构造函数const unsigned int中使用它时,编译器会发出警告。boost::gregorian::date

decltype(nMinYear) nMinYear1 = nMinYear + 1;

是一个修复。


推荐阅读