为什么我在使用 nullptr_t 时收到 "parameter set but not used" 警告?
Why am I getting a "parameter set but not used" warning when using nullptr_t?
我有一个自定义 class 实现 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;
}
代码按预期工作,但 g++(6.2.1 版)给出了以下警告:
[Timur@Timur-Zenbook misc]$ g++ aaa.cpp -o aaa -Wall -Wextra
aaa.cpp: In member function ‘bool C::operator==(std::nullptr_t) const’:
aaa.cpp:12:36: warning: parameter ‘n’ set but not used [-Wunused-but-set-parameter]
bool operator==(std::nullptr_t n) const {
^
我做错了什么?
注意:我正在使用 -Wall -Wextra
。
对于为什么会发生这种情况并没有真正的答案,但无论如何什么值可以 n
但是 nullptr
?
返回 this->v == nullptr
并使参数未命名会删除警告:
bool operator==(std::nullptr_t) const {
return this->v == nullptr;
}
编辑:
将 n
声明为右值引用或常量左值引用也会删除警告:
bool operator==(std::nullptr_t&& n) const {
return this->v == n;
}
bool operator==(const std::nullptr_t& n) const {
return this->v == n;
}
EDIT2:
可以在 this question 中找到更多消除未使用变量警告的方法(感谢@ShafikYaghmour 在评论中指出)。上面的示例涵盖了 "implicit" 方法。
有明确的解决方案,但恕我直言,由于有效地使用了参数,因此看起来不太连贯。经过测试的显式解决方案包括:
bool operator==(std::nullptr_t n) const {
(void)n;
return this->v == n;
}
#define UNUSED(expr) do { (void)(expr); } while (0)
bool operator==(std::nullptr_t n) const {
UNUSED(n);
return this->v == n;
}
GCC 的非便携式解决方案:
bool operator==(__attribute__((unused)) std::nullptr_t n) const {
return this->v == n;
}
我有一个自定义 class 实现 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;
}
代码按预期工作,但 g++(6.2.1 版)给出了以下警告:
[Timur@Timur-Zenbook misc]$ g++ aaa.cpp -o aaa -Wall -Wextra
aaa.cpp: In member function ‘bool C::operator==(std::nullptr_t) const’:
aaa.cpp:12:36: warning: parameter ‘n’ set but not used [-Wunused-but-set-parameter]
bool operator==(std::nullptr_t n) const {
^
我做错了什么?
注意:我正在使用 -Wall -Wextra
。
对于为什么会发生这种情况并没有真正的答案,但无论如何什么值可以 n
但是 nullptr
?
返回 this->v == nullptr
并使参数未命名会删除警告:
bool operator==(std::nullptr_t) const {
return this->v == nullptr;
}
编辑:
将 n
声明为右值引用或常量左值引用也会删除警告:
bool operator==(std::nullptr_t&& n) const {
return this->v == n;
}
bool operator==(const std::nullptr_t& n) const {
return this->v == n;
}
EDIT2:
可以在 this question 中找到更多消除未使用变量警告的方法(感谢@ShafikYaghmour 在评论中指出)。上面的示例涵盖了 "implicit" 方法。
有明确的解决方案,但恕我直言,由于有效地使用了参数,因此看起来不太连贯。经过测试的显式解决方案包括:
bool operator==(std::nullptr_t n) const {
(void)n;
return this->v == n;
}
#define UNUSED(expr) do { (void)(expr); } while (0)
bool operator==(std::nullptr_t n) const {
UNUSED(n);
return this->v == n;
}
GCC 的非便携式解决方案:
bool operator==(__attribute__((unused)) std::nullptr_t n) const {
return this->v == n;
}