C++ 在MSVC上,为什么会发出警告;未引用的形式参数";没有为std::string显示?

C++ 在MSVC上,为什么会发出警告;未引用的形式参数";没有为std::string显示?,c++,visual-c++,parameters,compiler-warnings,stdstring,C++,Visual C++,Parameters,Compiler Warnings,Stdstring,我有一个带有三个未使用参数的函数: #include <string> void Test(int b, std::string a, int c) { } int main() { return 0; } 为什么我得不到与std::string a相同的警告?虽然我无法回答为什么它的行为是这样的,但我注意到有一种模式 只要析构函数不是默认值,MSVC就不会对未使用的对象发出警告: struct X { ~X();// = default; }; void T

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

#include <string>

void Test(int b, std::string a, int c)
{
}

int main()
{
    return 0;
}

为什么我得不到与
std::string a
相同的警告?

虽然我无法回答为什么它的行为是这样的,但我注意到有一种模式

只要析构函数不是默认值,MSVC就不会对未使用的对象发出警告:

struct X {
    ~X();// = default;
};

void Test(int b, X x)
{

}

int main()
{
    return 0;
}
此代码不会对
x
发出警告,但如果取消注释,则会出现
=default
警告


我不确定这是一个功能(例如,考虑到破坏一个对象的潜在副作用)还是一个分析器的工件。

这是一个依赖于实现的警告

我已经在gcc和clang上测试了您的代码,它们都警告a、b和c:

叮当声:

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.
通用条款:


所以,也许这只是msvc的疏忽

看起来调用析构函数阻止了警告。我不知道是哪个编译器,但很可能是因为
a
有一个非默认构造函数,所以按值传递时可能会产生副作用。它很可能是防止编译器抱怨
std::lock\u guard
在某个范围内未使用的相同逻辑-副作用很重要!当析构函数是非平凡的时忽略警告的逻辑是错误的,即使出于某种原因,您只需要为析构函数使用变量,它不需要有名称。如果指定了名称,但没有提及,它看起来仍然像没有平凡的析构函数一样破碎。问题是为什么MSVC会以这种特定的方式运行,我不确定其他编译器的结果加上“可能只是疏忽”是一个答案。抱歉,但这并不是对这个问题的回答,第一行是答案:由实现决定是否发出警告。它可能是正确的,也可能不是。其他编译器发出的警告是作为额外信息提供的。但问题是,MSVC在不同情况下是否发出警告这一点上为什么不一致,这意味着需要一个更具体的答案,而不是“好吧,你实际上不需要得到任何警告”和引用其他编译器产生其他结果。从技术上讲,这是唯一一个真正有答案的:它是不同的,因为它是被允许的(与bug相反)。他们可能在寻找一个为什么,最好的答案是两个当前副本的组合,但这实际上回答了这个问题。非默认副本构造函数是否也会阻止警告?这将是一致的,因为这也是通过值传递调用的结果。@TobySpeight令我惊讶的是,在我执行的测试中,非默认复制构造函数并没有起到任何作用。可能是因为它是在调用站点上调用的,而不是析构函数。
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.
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)
                        ~~~~~~~~~~~~^