c++ - 带有警告 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);
但我不明白为什么会发生警告。
解决方案
的类型
nMinYear + 1
将是unsigned int
由于该表达式中术语的隐含扩展。nMinYear1
a 也是如此,当在构造函数const unsigned int
中使用它时,编译器会发出警告。boost::gregorian::date
decltype(nMinYear) nMinYear1 = nMinYear + 1;
是一个修复。
推荐阅读
- c++ - 在类中初始化 unique_ptr
- jupyter-notebook - 如何使用此 python 代码解决关键错误?
- amazon-web-services - 如果我不添加默认变量,则 Terraform/Terragrunt 错误
- python - Django 3:无法生成动态对象视图
- html - 动画旋转框 css 关键帧
- node.js - 如何在 MongoDB 中相互比较文档?
- c# - 从蛇案到骆驼案的对象序列化
- python - Pyspark Mutlipe 数据框初始化
- api - 如何避免 Api 平台(Symfony)中重复条目的 SQL 异常?
- elasticsearch - Elasticsearch - TikaOnDotNet 逐页文本提取