在Visual Studio中分别抑制C4189

Suppress C4189 in Visual Studio for each

本文关键字:C4189 Visual Studio      更新时间:2023-10-16

我在Visual Studio 2013上工作,得到了一个简单的循环

for each (const std::string &foo in stringList)
{ 
    /*Things happen here where foo is never used for*/
}

在循环中,我从列表中弹出N个元素,所以我不在这里使用foo,并得到C4189警告(w4),我想在这里抑制。

我环顾四周,发现了几个解决方案,如

for each (const std::string &foo in stringList)
{ 
    (void)foo;
    /*Things happen here where foo is never used for*/
}

或定义像#define UNUSED __pragma(warning(suppress:4189))

这样的解

我个人喜欢define解决方案,它似乎不能在循环中工作,因为如果我使用define,我仍然会得到警告。

我的问题是:

是否有一种方法可以在不设置编译器标志的情况下抑制警告?我不希望在循环中使用额外的行来抑制警告。

c++ 17起:

    for ([[maybe_unused]] const auto &foo in stringList)
{ 
    /*Things happen here where foo is never used for*/
}