Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. New [[nodiscard]] attribute usage.

New [[nodiscard]] attribute usage.

Scheduled Pinned Locked Moved C / C++ / MFC
question
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Maximilien
    wrote on last edited by
    #1

    The [[nodiscard]] attribute can be use either for function return and for struct. Both will return the same warning; Is there a better use case for the attribute ? Either tag the struct:

    struct [[nodiscard]] Patate
    {
    int patate;
    };

    Patate f1()
    {
    return Patate();
    };

    void main()
    {
    f1(); // warning C4834: discarding return value of function with 'nodiscard' attribute.
    }

    or tag the function return.

    struct Patate
    {
    int patate;
    };

    [[nodiscard]] Patate f1()
    {
    return Patate();
    };

    void main()
    {
    f1(); // warning C4834: discarding return value of function with 'nodiscard' attribute.
    }

    I'd rather be phishing!

    J 1 Reply Last reply
    0
    • M Maximilien

      The [[nodiscard]] attribute can be use either for function return and for struct. Both will return the same warning; Is there a better use case for the attribute ? Either tag the struct:

      struct [[nodiscard]] Patate
      {
      int patate;
      };

      Patate f1()
      {
      return Patate();
      };

      void main()
      {
      f1(); // warning C4834: discarding return value of function with 'nodiscard' attribute.
      }

      or tag the function return.

      struct Patate
      {
      int patate;
      };

      [[nodiscard]] Patate f1()
      {
      return Patate();
      };

      void main()
      {
      f1(); // warning C4834: discarding return value of function with 'nodiscard' attribute.
      }

      I'd rather be phishing!

      J Offline
      J Offline
      John R Shaw
      wrote on last edited by
      #2

      Your function is returning by value, which your code is ignoring (discarding). A Google search for "attribute nodiscard" provides the answer. C++ attribute: nodiscard (since C++17) - cppreference.com[^]

      INTP "Program testing can be used to show the presence of bugs, but never to show their absence." - Edsger Dijkstra "I have never been lost, but I will admit to being confused for several weeks. " - Daniel Boone

      M 1 Reply Last reply
      0
      • J John R Shaw

        Your function is returning by value, which your code is ignoring (discarding). A Google search for "attribute nodiscard" provides the answer. C++ attribute: nodiscard (since C++17) - cppreference.com[^]

        INTP "Program testing can be used to show the presence of bugs, but never to show their absence." - Edsger Dijkstra "I have never been lost, but I will admit to being confused for several weeks. " - Daniel Boone

        M Offline
        M Offline
        Maximilien
        wrote on last edited by
        #3

        I know that, but the description is not clear about the differences if any between the nodiscard for the struct vs for the function.

        I'd rather be phishing!

        L L 2 Replies Last reply
        0
        • M Maximilien

          I know that, but the description is not clear about the differences if any between the nodiscard for the struct vs for the function.

          I'd rather be phishing!

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Well, That attribute is weakly defined in section 10.6.7 of the standard. You can go read it yourself. The behavior is intentionally vague using words like 'encouraged' and 'discouraged'. Regardless of whether it's applied to a function or class there is no difference defined. In fact if you carefully read the document no behavior has been defined. The only section without 'encouraged' and 'discouraged' is section 1 where the attribute token "[[nodiscard]]" itself is defined. In other words in C++17 the only guarantee is that your compiler understand the [[nodiscard]] attribute. No behavior is defined. Best Wishes, -David Delaune

          1 Reply Last reply
          0
          • M Maximilien

            I know that, but the description is not clear about the differences if any between the nodiscard for the struct vs for the function.

            I'd rather be phishing!

            L Offline
            L Offline
            leon de boer
            wrote on last edited by
            #5

            There is no difference you are discarding something (you didn't save it) that was marked as non discard. The whole idea should have been euthanized, it is what you get when you get a committee to talk about non existent problems. There are half a dozen real problems but they waste time on that junk. Here is the best template to fix [nodiscard]

            template < typename T >
            void ignore(T&&) {}

            Try it on your code

            ignore(f1());

            Now don't make me do it .... if I want to discard something I will :-)

            In vino veritas

            J 1 Reply Last reply
            0
            • L leon de boer

              There is no difference you are discarding something (you didn't save it) that was marked as non discard. The whole idea should have been euthanized, it is what you get when you get a committee to talk about non existent problems. There are half a dozen real problems but they waste time on that junk. Here is the best template to fix [nodiscard]

              template < typename T >
              void ignore(T&&) {}

              Try it on your code

              ignore(f1());

              Now don't make me do it .... if I want to discard something I will :-)

              In vino veritas

              J Offline
              J Offline
              John R Shaw
              wrote on last edited by
              #6

              LOL :laugh: I think someone got tired of programmers who do not follow the good practice of checking returned values and decided to provide a standard way to warn them. The problem with that is those same programmers also tend to ignore warnings. ;)

              INTP "Program testing can be used to show the presence of bugs, but never to show their absence." - Edsger Dijkstra "I have never been lost, but I will admit to being confused for several weeks. " - Daniel Boone

              R 1 Reply Last reply
              0
              • J John R Shaw

                LOL :laugh: I think someone got tired of programmers who do not follow the good practice of checking returned values and decided to provide a standard way to warn them. The problem with that is those same programmers also tend to ignore warnings. ;)

                INTP "Program testing can be used to show the presence of bugs, but never to show their absence." - Edsger Dijkstra "I have never been lost, but I will admit to being confused for several weeks. " - Daniel Boone

                R Offline
                R Offline
                Rajesh R Subramanian
                wrote on last edited by
                #7

                John R. Shaw wrote:

                ... same programmers also tend to ignore warnings.

                But, but, if it compiles, I'd then not need to worry about the warnings, right?!

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups