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

            Bug ID: 89070
           Summary: Attribute [[nodiscard]] should be ignored in
                    unevaluated contexts
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: d.frey at gmx dot de
  Target Milestone: ---

Title says it all. In an unevaluated context, the result is never used anyways.
Other compilers do ignore [[nodiscard]] in those contexts already. Example
code:

struct A
{
    [[nodiscard]] static int match() { return 42; }
};

template<typename T>
auto g() -> decltype( T::match(), bool() )
{
    return T::match();
}

int main()
{
    g<A>();
}

Leads to:

<source>: In substitution of 'template<class T> decltype ((T::match(), bool()))
g() [with T = A]':

<source>:14:10:   required from here

<source>:7:31: warning: ignoring return value of 'static int A::match()',
declared with attribute nodiscard [-Wunused-result]

    7 | auto g() -> decltype( T::match(), bool() )

      |                       ~~~~~~~~^~

<source>:3:30: note: declared here

    3 |     [[nodiscard]] static int match() { return 42; }

      |                              ^~~~~

Compiler returned: 0

See https://godbolt.org/z/OQEnaD

Reply via email to