C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Attribute [[discard]] and attributes on expressions

From: Giuseppe D'Angelo <giuseppe.dangelo_at_[hidden]>
Date: Fri, 6 Oct 2023 01:06:02 +0200
Il 05/10/23 23:39, Julien Jorge via Std-Proposals ha scritto:
> Attributes can be safely ignored without changing the semantics of the program, so what does it means to have a statement like `int a = foo(), [[discard]] bar()`? In any case `a` must receive the result of `foo()`, isn't it?

I'm not sure how to read this question:

Right now, without attributes, `int a = foo(), bar();` is the Most
Vexing Parse in action. It's declaring an `int a` and initializing it by
calling `foo()`, and it's also declaring `int bar()` (a function). The
attribute you've placed should appertain to the return type of the
function, and not any expression.


Changing the example to:

int a = (foo(), [[discard]] bar());

then under my proposal this would be ill-formed: I'm proposing
attributes on the right hand side of expressions only. For instance like
this:

int a = (foo(), bar() [[discard]]);


Which should work. The semantics are the same as if [[discard]] were not
there: foo() is evaluated and discarded, bar() is evaluated and used to
initialize `a`. In any case `a` does *not* receive `foo()`. In this
example, the attribute is meaningless, as it's meant to suppress a
nodiscard warning that you would not get anyhow. Are you suggesting to
"enforce" that [[discard]] should not be applied to expressions that
aren't discarded-value expressions?

Thank you,
-- 
Giuseppe D'Angelo

Received on 2023-10-05 23:06:05