例如:
Bool NullFunc(const struct timespec *when, const char *who)
{
return TRUE;
}
Run Code Online (Sandbox Code Playgroud)
在C++中,我能够对/*...*/参数进行评论.但当然不是在C中,它给了我错误error: parameter name omitted.
我想知道你传递给gcc编译器的什么开关来关闭未使用的变量警告?我在Windows上获得了错误,我不想触摸增强代码:
C:\boost_1_52_0/boost/system/error_code.hpp: At global scope:
C:\boost_1_52_0/boost/system/error_code.hpp:214:36: error: 'boost::system::posix_category' defined but not used [-Werror=unused-variable]
C:\boost_1_52_0/boost/system/error_code.hpp:215:36: error: 'boost::system::errno_ecat' defined but not used [-Werror=unused-variable]
C:\boost_1_52_0/boost/system/error_code.hpp:216:36: error: 'boost::system::native_ecat' defined but not used [-Werror=unused-variable]
Run Code Online (Sandbox Code Playgroud)
我尝试使用两者-Wunused-value,-Wno-unused-value但都没有压制上面的消息.
什么是正确的命令,这是我的编译行:
g++ -g -fno-inline -Wall -Werror -Wextra -Wfloat-equal -Wshadow
-Wpointer-arith -Wcast-qual -Wcast-align -Wwrite-strings -Wno-conversion
-Wdisabled-optimization -Wredundant-decls -Wunused-value -Wno-deprecated
-IC:\\boost_1_52_0 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
-c -o op.o op.cpp
Run Code Online (Sandbox Code Playgroud)
也许这-Wall超越了我的目标?
序幕:
std::tuple<int, int, int> f();
std::tuple<int, int, float, int> g();
Run Code Online (Sandbox Code Playgroud)
C++ 1z将引入结构化绑定的语法,这样可以编写代替
int a, b, c;
std::tie(a, b, c) = f();
Run Code Online (Sandbox Code Playgroud)
就像是
auto [a, b, c] = f();
Run Code Online (Sandbox Code Playgroud)
但是,std::tie也允许指定std::ignore忽略某些组件,例如:
std::tie(a, b, std::ignore, c) = g();
Run Code Online (Sandbox Code Playgroud)
是否可以使用新的结构化绑定语法执行类似的操作?它会如何工作?
有时局部变量的唯一目的是在assert()中检查它,就像这样 -
int Result = Func();
assert( Result == 1 );
Run Code Online (Sandbox Code Playgroud)
在Release版本中编译代码时,通常会禁用assert(),因此此代码可能会生成有关Result已设置但从未读取的警告.
可能的解决方法是 -
int Result = Func();
if ( Result == 1 )
{
assert( 0 );
}
Run Code Online (Sandbox Code Playgroud)
但它需要太多的打字,在眼睛上并不容易,并且导致总是检查条件(是的,编译器可以优化检查,但仍然).
我正在寻找一种替代方法来表达这个assert()的方式不会导致警告,但仍然易于使用并避免更改assert()的语义.
(在此区域代码中使用#pragma禁用警告不是一种选择,并且降低警告级别以使其消失也不是一种选择......).
抑制编译器(在本例中为gcc)的最佳/最佳方法是什么,如"未使用的变量x" - 警告?
我不想给gcc提供任何特定的标志来删除所有这些警告,仅用于特殊情况.
用它std::ignore来忽略未使用的变量是一种好方法吗?
假设我有这样的函数:
void func(int i)
{
//for some reason, I don't need i anymore but I cannot change signature of function
std::ignore = i;
}
Run Code Online (Sandbox Code Playgroud)
附加信息
这是一个例子,一些答案建议使用匿名变量.但是我如何为其他情况做这件事,比如:
int Thread_UnSafe_func_returnSomething():
void func()
{
// To make it thread safe
// Also it is required to call only once
static int i = Thread_UnSafe_func_returnSomething();
std::ignore = i;
}
Run Code Online (Sandbox Code Playgroud) 在gcc/g ++ 4.9下我可以写:
int x __attribute__((unused)) = f();
Run Code Online (Sandbox Code Playgroud)
表示x是故意未使用的.
是否有可能以[[]]某种方式使用C++ 11 属性表示法?
我试过了:
int x [[unused]] = f();
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
(是的,我知道它是一个实现定义的属性.)
我在libmagic代码中找到了这些行,(sourceforge.net/projects/libmagic/files/latest/download).他们的意思是什么?什么是python等价物?
#ifdef __GNUC__
__attribute__((unused))
#endif
Run Code Online (Sandbox Code Playgroud)
什么__GNUC__意思?
所以它似乎检查是否安装了GCC编译器,是什么意思__CC_ARM,__ ICCARM __,__ GNUC __,_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
什么是__attribute__((unused))?
这里有一段代码片段,但没有解释:我如何最好地消除有关未使用变量的警告?
是什么样的区别__GNUC__,并_MSC_VER
有一定的解释上_MSC_VER,但它是什么一回事呢?如何检测我是否使用Visual Studio 2008编译代码?
最后一个问题:我怎样才能#ifdef检查操作系统是否正在使用GNU和MS可视化工作室编译我的python代码?
在以下示例中:
struct Foo {
[[maybe_unused]] int member = 1;
void bar() {
[[maybe_unused]] int local = 0;
}
};
int main(int argc, char* argv[]) {
Foo f{};
f.bar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
GCC 发出警告,其中 Clang 和 MSVC 没有:
warning: 'maybe_unused' attribute ignored [-Wattributes]
[[maybe_unused]] int member = 1;
Run Code Online (Sandbox Code Playgroud)
据我所知,这应该是合法的(并且不会被编译器忽略)。根据标准:
10.6.7 可能未使用的属性 [dcl.attr.unused]
...
2. 该属性可以应用于类的声明、typedef-name、变量、非静态数据成员、函数、枚举,或枚举器。
...
我讨厌在“编译器错误”锤子上摇摆不定,但我不确定在这种情况下还有什么可能。
有没有人有任何见解?
我有一个自定义类实现operator==用nullptr.
这是我的代码愚蠢到一个简单的例子:
#include <cstdint>
#include <iostream>
class C {
private:
void *v = nullptr;
public:
explicit C(void *ptr) : v(ptr) { }
bool operator==(std::nullptr_t n) const {
return this->v == n;
}
};
int main()
{
uint32_t x = 0;
C c(&x);
std::cout << (c == nullptr ? "yes" : "no") << std::endl;
C c2(nullptr);
std::cout << (c2 == nullptr ? "yes" : "no") << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
代码按预期工作,但g ++(版本6.2.1)给出了以下警告:
[Timur@Timur-Zenbook misc]$ g++ aaa.cpp -o …Run Code Online (Sandbox Code Playgroud) c++ ×7
gcc ×5
c ×3
c++11 ×3
c++17 ×2
warnings ×2
assertions ×1
c++14 ×1
compiler-bug ×1
function ×1
g++ ×1
gcc-warning ×1
nullptr ×1
principles ×1