在 MSVC 上,为什么没有出现 std::string 的警告"unreferenced formal parameter"?

On MSVC why does the warning "unreferenced formal parameter" does not appear for std::string?

本文关键字:警告 string unreferenced parameter formal std MSVC 为什么      更新时间:2023-10-16

我有一个函数,带有三个未使用的参数:

#include <string>
void Test(int b, std::string a, int c)
{
}
int main()
{
    return 0;
}

在Visual Studio 2017中的4级上编译时,我会收到bc的警告:

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的疏忽。