C++ Logo

std-proposals

Advanced search

Re: [std-proposals] New function attribute [[nodiscard_scope]]

From: Julien Villemure-Fréchette <julien.villemure_at_[hidden]>
Date: Fri, 30 Jun 2023 14:22:05 -0400
> That is to say, all variables are objects, but only some
objects are variables. But maybe the C++ Standard has a concise
definition of the two terms (it it has, I haven't read it).

Yes, it does. Both of the terms 'object' and 'variable' are defined in section 6 Basics.
There is a hole section that describes the Object Model https://eel.is/c++draft/intro.object

And the term variable is defined precisely at
https://eel.is/c++draft/basic#def:variable

Remark: variables can have an object type or a reference type (including reference to function); and a reference is not an object. Hence, it is not the case that all variable are objects.

> The following should
be a warning/error though:

    auto &ref = *g_obj.synchronize();

because it obviously violates the intended scoping of the return value.

Could you elaborate on how it violates the intended scoping of the return value? I don't see what is the "intended scoping". Usually in c++, expression of lifetime is best expressed directly by defining objects in the appropriate scope.

Julien V.

On June 30, 2023 3:18:57 a.m. EDT, Frederick Virchanza Gotham via Std-Proposals <std-proposals_at_[hidden]> wrote:
>On Thu, Jun 29, 2023 at 10:37 PM Jason McKesson via Std-Proposals
><std-proposals_at_[hidden]> wrote:
>>
>> ```
>> some_func(g_obj.synchronize()); //Should only synchronize for the
>> duration of the function call.
>> ```
>
>
>This is why I called it [[nodiscard_scope]]. It means that you don't
>discard the return value, and also that you are careful about its
>scope.
>I think the following two should be permissible:
>
> some_func( g_obj.synchronize());
> some_func(*g_obj.synchronize());
>
>because the scoping of the return value is okay. The following should
>be a warning/error though:
>
> auto &ref = *g_obj.synchronize();
>
>because it obviously violates the intended scoping of the return value.
>
>With regard to terminology, I think many people including myself
>define the word 'object' as a synonym for 'variable' -- except that
>the former is more typically used for classes, and that the latter is
>used exclusively for intrinsic types (int, double, float, short,
>etc.). That is to say, all variables are objects, but only some
>objects are variables. But maybe the C++ Standard has a concise
>definition of the two terms (it it has, I haven't read it).
>--
>Std-Proposals mailing list
>Std-Proposals_at_[hidden]
>https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2023-06-30 18:22:16