Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Does GCC 7.3 omit the [[nodiscard]] attribute for reference returning member functions?

I’ve got the following code utilizing the [[nodiscard]] attribute of C++17.

class SomeClass {
public: /** Methods **/
    [[nodiscard]] int getValue()  { return n; }
    [[nodiscard]] int &getRef()   { return n; }
    [[nodiscard]] int *getPtr()   { return &n; }

private: /** Members **/
    int n{5};
};

int main() 
{
    SomeClass object;

    object.getValue();
    object.getRef();
    object.getPtr();

    return 0;
}

When I compile it with GCC 7.3, I’ve two warnings stating that the return value of two functions is ignored. The two functions detected by the compiler are the ones that don’t return a reference, getValue() and getPtr().

On the other hand, when compiled with GCC 8.1 and above versions, the getRef() also causes a warning.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The C++ support table provided by GCC shows that the [[nodiscard]] attribute is fully supported as of version 7. It also has a white paper.

Appearance of a [[nodiscard]] call as a potentially ­evaluated discarded­ value expression is discouraged unless explicitly cast to void.

So, is it a bug or am I missing something?

>Solution :

Yes, it is a bug. It was fixed in GCC 8 as you have already realized.

Bug report: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80896

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading