boost::gregorian::date 带有警告 C4244 的构造函数

boost::gregorian::date constructor with Warning C4244

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

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 构造行。

Warning C4244 'argument': conversion from 'unsigned int' to 'unsigned short', possible loss of data

我的测试项目在 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);

但我不明白为什么会出现警告。

类型

nMinYear + 1

将是 unsigned int,因为该表达式中的术语隐式扩大。所以 nMinYear1 是一个 const unsigned int,编译器在 boost::gregorian::date 构造函数中使用它时会发出警告。

decltype(nMinYear) nMinYear1 = nMinYear + 1;

是一个修复。