来自 std::chrono 的编译器警告,但未被使用

Compiler warning from std::chrono but is not being used

本文关键字:警告 std chrono 编译器 来自      更新时间:2023-10-16

注意:此错误仅发生在发布和调试模式下的 x64 项目上。

涉及std::chrono的奇怪警告出现在这段代码上,使用 VC2019 处于警告级别 3。这是一段精简的代码,用于处理命令行标志。我已经删除了大部分与问题无关的胆量。

#if 1   // enable bug
#include <chrono>   // excluding this also eliminates chrono warnings
using CorrectedIntType=int;
#else
using CorrectedIntType=size_t;
#endif
#include <iostream>
#include <vector>
#include <string>
#include <type_traits>
using std::vector;
using std::string;
namespace {
void fixup(const std::string& argcmd, std::string& arg) { arg = argcmd; }
template<class T>
void procVal(std::vector<std::string>& arglist, CorrectedIntType idx, T& arg)
{
fixup(arglist[idx], arg);
arglist.erase(arglist.begin() + idx);
}
template<class T, class ...TA>
void procVal(std::vector<std::string>& arglist, CorrectedIntType idx, T& arg, TA&...argv)
{
procVal(arglist, idx, arg);
procVal(arglist, idx, argv...);
}
template<class T, class ...TA>
bool procFlag(const char* pc, std::vector<std::string>& arglist, T& arg1, TA&...argv)
{
std::string flag(pc);
for (size_t i = 0; i < arglist.size(); i++)
{
if (arglist[i] == flag)
{
arglist.erase(arglist.begin() + i);
procVal(arglist, i, arg1);      // process one argument after flag
return true;
}
}
return false;
}
}
int main()
{
string outfile;
vector<string> test = { "test" };
procFlag("-o", test, outfile);      // assigns test[0] to outfile and removes it
std::cout << outfile << 'n';
}

警告:

1>Source.cpp
1>C:UsersmgrayDocumentsVisual Studio 2017ProjectsCommandLineCPPstackoverflowSource.cpp(35,1): warning C4267: 'argument': conversion from 'size_t' to 'CorrectedIntType', possible loss of data
1>C:UsersmgrayDocumentsVisual Studio 2017ProjectsCommandLineCPPstackoverflowSource.cpp(54): message : see reference to function template instantiation 'bool `anonymous-namespace'::procFlag<std::string,>(const char *,std::vector<std::string,std::allocator<std::string>> &,T &)' being compiled
1>        with
1>        [
1>            T=std::string
1>        ]
1>C:Program Files (x86)Microsoft Visual Studio2019EnterpriseVCToolsMSVC14.24.28314includechrono(632): message : see reference to class template instantiation 'std::chrono::duration<double,std::ratio<1,1>>' being compiled
1>C:Program Files (x86)Microsoft Visual Studio2019EnterpriseVCToolsMSVC14.24.28314includechrono(178): message : see reference to class template instantiation 'std::chrono::duration<__int64,std::nano>' being compiled
1>C:Program Files (x86)Microsoft Visual Studio2019EnterpriseVCToolsMSVC14.24.28314includechrono(610): message : see reference to class template instantiation 'std::chrono::time_point<std::chrono::steady_clock,std::chrono::nanoseconds>' being compiled

虽然代码有效,但即使存在int -<> size_t转换问题(这是一个合法的警告(,当顶部的宏设置为 0 时,所有警告都会消失。因此,不知何故,size_t 和 int 之间的大小差异触发了时间消息。我担心chrono警告存在,因为它不涉及。这是VS2019中的错误吗?关于为什么会出现chrono警告引用的任何想法?

这是一个有效的警告,它与<chrono>无关,而是与您自己的代码和CorrectedIntType类型有关。这是一个没有<chrono>的简化代码 https://gcc.godbolt.org/z/qf9v8TEh7:

procVal的定义中,第二个参数是CorrectedIntType

void procVal(std::vector<std::string>& arglist, CorrectedIntType idx, T& arg)

但它是从具有size_t值的procFlag调用的:

bool procFlag(const char* pc, std::vector<std::string>& arglist, T& arg1)
...
for (size_t i = 0; i < arglist.size(); i++)
...
procVal(arglist, i, arg1);

因此,也可以通过将i类型更改为CorrectedIntType来修复警告。