我有一个带有三个未使用参数的函数:
#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
的相同警告?
答案 0 :(得分:3)
虽然我无法回答为什么,但我注意到有一种模式。
只要析构函数没有默认,看起来MSVC就不会对未使用的对象发出警告:
struct X {
~X();// = default;
};
void Test(int b, X x)
{
}
int main()
{
return 0;
}
此代码并未警告x
,但如果您取消注释,则会显示= default
警告。
我不确定它是否是一个特征(例如,考虑到破坏物体的潜在副作用)或分析仪的伪像。
答案 1 :(得分:0)
这是一个依赖于实现的警告。
我已经在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的疏忽。