我有一个应用程序,我必须在其中声明(覆盖)从接口继承的方法。但是,这些方法具有我的实现中未使用的参数。
class MyImplementation implements ISomething {
public function foo($bar) {
// code that does nothing with $bar
...
}
// other methods
...
}
Run Code Online (Sandbox Code Playgroud)
如何在源代码中将 $bar 标记为未使用,在 C++ 中这是可能的。
换句话说,我怎样才能在 PHP 中做到这一点?
假设我正在处理w44101默认启用警告的大型代码库.这意味着如果我进入我的项目并右键单击属性 - > C/C++ - >命令行 - >会/w44101显示在其他选项部分中.
我希望能够通过更改配置而不是源代码来禁用此警告.我尝试进入属性 - > C/C++ - >所有选项 - >禁用特定警告并输入4101,这实际上产生了一个/wd"4101"属性 - > C/C++ - >命令行.但是,当我编译我的项目时,它仍会抛出4101警告.为什么不/wd"4101"和/w44101互相抵消?
我在使用Visual Studio 2015的Windows 10上.禁用此警告的正确方法是什么?如果可以在CMake中使用某种类型的函数调用所提出的解决方案将是优选的,因为.sln该代码库的文件由CMake生成.
编辑:我正在处理的代码库默认情况下设置了严格的编译标志.它是用/W4和编译的/WX.还附加级别4的警告,仅举几个例子,例如,/w44101,/w44062,/w44191等.
我正在尝试编写一个宏,以便在用户需要时使用抑制未使用的变量警告(例如,在派生类中,当您尚未实现整个类时)。我知道我可以删除变量名称...但为了明确起见,我更喜欢宏)。
到目前为止我有这个:
#ifdef WIN32
#define UNUSED(x) x
#else
#define x __attribute__((unused))
#endif
Run Code Online (Sandbox Code Playgroud)
用法如下:
void test_fn(int UNUSED(test_var)) {...}
我看到这篇文章:suppressing-is-never-used-and-is-never-assigned-to-warnings-in-c-sharp,但它给了我一个我无法真正使用的结果(多行#pragmas)。
所以我的问题是,是否有相当于 MSVS 的__attribute__((unused))?- 即在同一条线上?
注意:这个问题没有回答如何做我所问的事情:how-do-i-best-silence-a-warning-about-unused-variables,因为它没有涵盖如何在函数原型中以某种方式使用它适用于 MSVS 和 gcc。
有没有办法抑制特定文件,命名空间或特定变量的"未使用的变量"警告?
我问,因为我有一个包含lambda函数列表的命名空间.有些现在没有使用,但可能会及时使用.如果这些是常规免费功能,如果有些未使用,我不会收到警告.但是,因为它们是lambda,所以我最终得到了一堆编译器警告.
我不希望使用编译器标志除去所有这些类型的警告,因为通常情况下,它是非常有用的编译器捕捉未使用的变量.但是,有关未使用的实用程序功能的一堆警告会将噪声添加到其他有用的信息中.
(void)var;对于类似于C/C++ 中的技巧的特定变量,是否有任何可移植的方法可以在 Fortran 中抑制“未使用的虚拟参数”警告?
一个激励性的例子(应弗拉基米尔·F 的要求)。策略模式,GoF示例,具有不同的断行策略,省略不必要的细节。
module linebreaking
type, abstract :: linebreaking_compositor
contains
procedure(linebreaking_compositor_compose), deferred, pass(this) :: compose
end type
abstract interface
subroutine linebreaking_compositor_compose(this)
import linebreaking_compositor
class(linebreaking_compositor), intent(in) :: this
end subroutine linebreaking_compositor_compose
end interface
type, extends(linebreaking_compositor) :: linebreaking_simple_compositor
contains
procedure, pass(this) :: compose => linebreaking_simple_compositor_compose
end type linebreaking_simple_compositor
type, extends(linebreaking_compositor) :: linebreaking_tex_compositor
contains
procedure, pass(this) :: compose => linebreaking_tex_compositor_compose
end type linebreaking_tex_compositor
type, extends(linebreaking_compositor) :: linebreaking_array_compositor
private
integer :: interval
contains
procedure, pass(this) :: compose => linebreaking_array_compositor_compose
end …Run Code Online (Sandbox Code Playgroud) 当我编译这段代码时,它说"错误C4700:未初始化的局部变量'b'使用". 我不知道我现在要做些什么来解决这个问题.我既不是IT学生也不是技术人员,但我非常喜欢学习C++,而且我自己也在学习.我已经在这一天了.
非常感谢
#include <stdio.h>
#include <iostream>
//A.
//1--
void InputArray(int *a, int &n)
{
printf("Insert n = ");
scanf("%d", &n);
a = new int[n];
for (int i=0; i<n; i++)
{
printf("Enter the key's a[%d] values: ", i);
scanf("%d",&a[i]);
}
}
void main()
{
int *b, m;
InputArray(b, m);
}
Run Code Online (Sandbox Code Playgroud) 我正在清理给定代码的所有警告.(这意味着我并不完全理解每一行逻辑)."[-Wunused-parameter]"已启用且不应关闭.
对于"未使用的参数",我们通常只使用"_ _ attribute _ _((unused))".
对于以下功能,我不能使用上述技巧.
void fun(int* a =0){
----variable ptr a is not used at all----
}
Run Code Online (Sandbox Code Playgroud)
有一个干净的解决方案去除警告的任何好主意?
我正在尝试解决由许多未使用的变量生成的 C++ 项目中的大量警告。例如,考虑这个函数:
void functionOne(int a, int b)
{
// other stuff and implementations :D
doSomethingElse();
runProcedureA();
}
Run Code Online (Sandbox Code Playgroud)
在我的项目中,为了抑制警告,我只是将未使用的变量强制转换为void,因为我无法更改方法签名。
void functionOne(int a, int b)
{
(void)a;
(void)b;
// other stuff and implementations :D
doSomethingElse();
runProcedureA();
}
Run Code Online (Sandbox Code Playgroud)
这项技术工作正常,但我有大量的功能需要执行此操作以解决警告问题。有没有办法通过将所有未使用的参数强制转换为void来自动重构所有这些函数?
目前,我正在使用 CLion IDE 和 VSCODE。