jeudi 24 novembre 2016

Why am I getting a "parameter set but not used" warning when using nullptr_t?

I have a custom class implementing operator== with nullptr.

Here is my code dumbed down into a simple example:

#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;
}

The code works as expected but g++ (version 6.2.1) gives me the following warning:

[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 {
                                    ^

What am I doing wrong?

NOTE: I'm using -Wall -Wextra.

Aucun commentaire:

Enregistrer un commentaire