I am once again reminded how much of a clusterfuck C++ as a whole is.

They recently (C++17) added this cool new attribute called nodiscard.
You can put it on a function like this

[[nodiscard]] bool writeMessage(...)

and the compiler will check if the returned value was discarded or not, and give a warning accordingly.
Pretty neat if you're returning an error code and you want to enforce that it gets checked, right?

Except it doesn't work in template classes.
It just doesn't do anything there.

  • 3
    And before any comments it:

    yes I know you can put [[nodiscard]] on a struct/class as well.

    But you can't do that with a typedef or using, so you have to define a dedicated struct/class that wraps around the value that you want to actually return.

    And I think that's a load of bullshit
  • 7
    It seems to be designed to only be in the context of a return value. So I can see why it would not make sense in a typedef. Its a compile warning flag, not actually something that affects the definition of the type.

    using RetType = struct {/*blah*/};

    RetType [[nodiscard]] funct(); // wouldn't this work?

    For reference:

  • 5
    You said it: C++ is a clusterfuck. I would add it to my bio but it has enough.
  • 7
    @rantsauce That would be ClusterFuck++
  • 1
    @Demolishun normally you would just put it on the function, but this does not work for functions inside template classes.


    the other option is to put the nodiscard on the data type that you return, aka

    struct [[nodiscard]] MyRetType {...}

    MyRetType someFunction()

    But that required me to always use custom structs/classes, which would then also change the way I access the values and may add overhead etc.

    the typedef/using thing I mentioned was one of my first attempts, I thought I might get away with something like this

    using NodiscardBool = [[nodiscard]] bool;

    NodiscardBool myFunction();

    but that caused a warning saying the attribute is ignored.

    The nodiscard feature is a great idea but I think this is a "good plan - horrible execution" situation.
  • 1
    @rantsauce It's in my bio now :)
  • 2
    I think that instead of "nodiscard" a better solution would have been to add a compiler flag to show a warning for any discarded return values.
    Then either add a "discard" as opt in, or provide a syntax for the call side to explicitly mark that returned value as discarded to silence the warning.
  • 1
    @Lensflare I think C used to do that, which is why sometimes you see people casting functions to void in order to suppress the warning

  • 0
    @LotsOfCaffeine That warning is not necessary in terms of the C standard - it may be due to whatever code checker you run over the source.

    That said, I like the (void) idiom to make clear that I know there's a return value, but decided to ignore it.
  • 1
    I think I’ve learned my lesson after working with it and OpenGL.

    Very much happy with Swift, thanks.
  • 0
    @Demolishun HAHAHAHA nice one m8
Add Comment