来自 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: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
来修复警告。
相关文章:
- cppcheck在const std::string[]上引发警告
- 关于std::move的使用,是否有编译警告
- 警告:忽略模板参数上的属性..在 std::unique_ptr (-wignore 属性)的声明中
- 来自 std::chrono 的编译器警告,但未被使用
- 警告级别为 3 的 int 的 std::vector push_back 处的编译器警告
- 静态 std::stringstream 的叮当整警告
- 捕获/禁止发送到 std::cout 的 OpenCV 警告
- 我可以在初始化之前使用 std::array 成员变量中的 data() 指针吗?发出警告
- 视觉 std::矢量无异常:警告 C4530:使用了C++异常处理程序,但未启用展开语义.指定 /EHsc
- 是否有 GCC 警告,用于使用 C 库中的符号而不是通过命名空间 std?
- G++ - 警告:扩展初始值设定项列表仅适用于 -std=C++11 或 -std=GNU++11
- 模板实例化警告使用std :: unordered_map
- Visual Studio 2015:std :: make_unique中没有签名/未签名的不匹配警告
- 如果对象在同一层次结构中,-Wreturn-std-move clang 警告是否正确
- 平息海湾合作委员会的"only available with -std=c++XX or -std=gnu++XX"警告
- 如何在代码块 IDE 上修复此警告,警告:按值 [-Wcatch-value=] 捕获多态类型"类 std::d omain_error"
- 在 MSVC 上,为什么没有出现 std::string 的警告"unreferenced formal parameter"?
- std::error_code 是发出警告的好方法吗?
- 消除"conversion to std::vector<int>::size_type to int"警告
- 警告在 std::move'ing 时调用复制 ctor