在 MSVC 上,为什么没有出现 std::string 的警告"unreferenced formal parameter"?
On MSVC why does the warning "unreferenced formal parameter" does not appear for std::string?
我有一个函数,带有三个未使用的参数:
#include <string>
void Test(int b, std::string a, int c)
{
}
int main()
{
return 0;
}
在Visual Studio 2017中的4级上编译时,我会收到b
和c
的警告:
1>consoleapplication2.cpp(8): warning C4100: 'c': unreferenced formal parameter 1>consoleapplication2.cpp(8): warning C4100: 'b': unreferenced formal parameter
为什么我不对std::string a
的警告?
虽然我无法回答为什么它的行为是这样的,但我注意到有一个模式。
看起来MSVC不会在未使用的对象上发出警告,只要destructor未默认:
struct X {
~X();// = default;
};
void Test(int b, X x)
{
}
int main()
{
return 0;
}
此代码不会警告x
,但是如果您输入= default
警告。
我不确定它是一个功能(例如,考虑破坏对象的潜在副作用)还是分析仪的工件。
这是一个与实现有关的警告。
我已经在GCC和Clang上测试了您的代码,都警告A,B和C:
clang:
prog.cc:5:15: warning: unused parameter 'b' [-Wunused-parameter]
void Test(int b, std::string a, int c)
^
prog.cc:5:30: warning: unused parameter 'a' [-Wunused-parameter]
void Test(int b, std::string a, int c)
^
prog.cc:5:37: warning: unused parameter 'c' [-Wunused-parameter]
void Test(int b, std::string a, int c)
^
3 warnings generated.
GCC:
prog.cc: In function 'void Test(int, std::__cxx11::string, int)':
prog.cc:5:15: warning: unused parameter 'b' [-Wunused-parameter]
void Test(int b, std::string a, int c)
~~~~^
prog.cc:5:30: warning: unused parameter 'a' [-Wunused-parameter]
void Test(int b, std::string a, int c)
~~~~~~~~~~~~^
prog.cc:5:37: warning: unused parameter 'c' [-Wunused-parameter]
void Test(int b, std::string a, int c)
~~~~~~~~~~~~^
所以,也许这只是MSVC的疏忽。
相关文章:
- 警告处理为错误这里有什么问题
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- cppcheck在const std::string[]上引发警告
- GCC对可能有效的代码抛出init list生存期警告
- 如何在BST的这个简单递归实现中消除警告
- 将std::string传递给WriteConsole API
- 关于std::move的使用,是否有编译警告
- g++ 在某个类成员未初始化时不发出警告
- 为std::string的某个索引赋值
- std中有类似find_last_of的函数,而string中没有
- 如何处理来自核心指南检查器的关于gsl::at的静态分析警告
- 使用 std::string () const 函数启动线程或未来
- 与 string.size() 比较时 int 的符号性显示警告
- q致命参数:将 QString 转换为常量字符* 会导致警告"format string is not a string literal"
- 在 MSVC 上,为什么没有出现 std::string 的警告"unreferenced formal parameter"?
- 为什么构造 std::string(0) 不会发出编译器警告
- FlexeLint/PC lint警告说,不考虑std::string::append的返回值
- 使用华大基因时"Deprecated conversion from string constant to 'char*'"警告?
- 在多线程环境中使用 std::string 时 Clang 的线程清理器警告
- std::string在Visual Studio 2010中给出编译器警告