C++ Logo

std-discussion

Advanced search

Re: Positioning the && after the ...

From: Andrey Semashev <andrey.semashev_at_[hidden]>
Date: Thu, 30 Nov 2023 15:31:42 +0300
On 11/30/23 14:32, Frederick Virchanza Gotham via Std-Discussion wrote:
> On Thu, Nov 30, 2023 at 10:58 AM Giuseppe D'Angelo wrote:
>>
>> Il 30/11/23 11:26, Frederick Virchanza Gotham via Std-Discussion ha scritto:
>>> The syntax of Func1 makes more sense because we have to define
>>> variables as follows:
>>>
>>> int &&a = monkey, &&b = donkey, &&c = fish;
>>>
>>> That is to say: The '&&' goes with the name -- not with the type.
>>
>> It goes with the type.
>
>
> No it doesn't.
>
> int array[64u];
>
> int (&array2)[64u] = array;
>
> If you want something to be a reference or a pointer, you put the
> asterisk or the ampersand with the _name_.

Whether you place it adjacent the name or type, the ampersand is part of
the type (that is, it denotes a reference type). Multideclarations are
bad exactly because they tear the type of the variable in two parts.

In your original example, you would still place the ampersands so that
they apply to the type, not the pack expansion. I.e. like this:

  template< typename... Args >
  void foo(Args&&... args);

Here, you expand what is left of the "..." operator, which is "Args&&",
which results in && applied to every type in the Args pack.

Writing it the other way doesn't make sense, as you can't apply a
reference to an expanded pack.

Received on 2023-11-30 12:31:45