https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84476

            Bug ID: 84476
           Summary: [[nodiscard]] ignored on virtual functions accessed
                    through pointer
           Product: gcc
           Version: 8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ixsci at yandex dot ru
  Target Milestone: ---

Given: A class with a virtual function which result should not be discarded and
a pointer to that class. 

Expected: Access to such a function w/o using its result should generate a
warning.

Actual result: no warning produced.

Code with comments where the warning is missing:

#include <memory>

using namespace std;

class A
{
public:
    [[nodiscard]] virtual int getVal()
    {
        return 0;
    }
    [[nodiscard]] int getVal2()
    {
        return 0;
    }
};

int main()
{
    auto a = make_unique<A>();
    // No warning
    a->getVal();
    // Warning
    a->getVal2();
    A aa;
    // Warning
    aa.getVal();
    // Warning
    aa.getVal2();
    A* aaa = new A{};
    // No warning
    aaa->getVal();
    // Warning
    aaa->getVal2();
    delete aaa;
};

Tested on Wandbox (gcc HEAD 8.0.1)

Reply via email to