来自 std::chrono 的编译器警告但未被使用
Compiler warning from std::chrono but is not being used
注意:此错误仅在发布和调试模式下的 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:\Users\mgray\Documents\Visual Studio 2017\Projects\CommandLineCPP\Whosebug\Source.cpp(35,1): warning C4267: 'argument': conversion from 'size_t' to 'CorrectedIntType', possible loss of data
1>C:\Users\mgray\Documents\Visual Studio 2017\Projects\CommandLineCPP\Whosebug\Source.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 Studio19\Enterprise\VC\Tools\MSVC.24.28314\include\chrono(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 Studio19\Enterprise\VC\Tools\MSVC.24.28314\include\chrono(178): message : see reference to class template instantiation 'std::chrono::duration<__int64,std::nano>' being compiled
1>C:\Program Files (x86)\Microsoft Visual Studio19\Enterprise\VC\Tools\MSVC.24.28314\include\chrono(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 时,所有警告都会消失。所以不知何故 [=25 之间的大小差异=] 和 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)
但它是从 procFlag
调用的 size_t
值:
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
来修复警告。
注意:此错误仅在发布和调试模式下的 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:\Users\mgray\Documents\Visual Studio 2017\Projects\CommandLineCPP\Whosebug\Source.cpp(35,1): warning C4267: 'argument': conversion from 'size_t' to 'CorrectedIntType', possible loss of data
1>C:\Users\mgray\Documents\Visual Studio 2017\Projects\CommandLineCPP\Whosebug\Source.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 Studio19\Enterprise\VC\Tools\MSVC.24.28314\include\chrono(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 Studio19\Enterprise\VC\Tools\MSVC.24.28314\include\chrono(178): message : see reference to class template instantiation 'std::chrono::duration<__int64,std::nano>' being compiled
1>C:\Program Files (x86)\Microsoft Visual Studio19\Enterprise\VC\Tools\MSVC.24.28314\include\chrono(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 时,所有警告都会消失。所以不知何故 [=25 之间的大小差异=] 和 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)
但它是从 procFlag
调用的 size_t
值:
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
来修复警告。