> Le 25 juil. 2022 à 16:55, Dan Eble <dan@lyric.works> a écrit :
>
> On Jul 24, 2022, at 19:04, Jean Abou Samra <j...@abou-samra.fr> wrote:
>>
>> I understand that ly_scm_list takes an rvalue and disallows
>> lvalues for clarity, while as_ly_scm_list takes an lvalue,
>> and will fail on an rvalue by design, with the failure being
>> a compilation failure since e67154f7e330b61c5d9a973fbb89bd56866a148e.
>>
>> But I don't understand the real difference that distinguishes
>> them. Before that commit, what would have been the difference
>> between as_ly_scm_list (lvalue) and ly_scm_list (lvalue)?
>
> The difference is ownership of the head.
>
> ly_scm_list has one data member:
>
> private:
> SCM head_ = SCM_EOL;
>
> Creating an instance of ly_scm_list creates this head. If a list operation
> modifies this head, no other SCM is modified.
>
> ly_scm_list x (my_things_); // A. x.head_ = my_things_
> ...
> x.clear (); // B. my_things_ is unchanged
>
> (B) might or might not be a problem. Without a comment, it can require deep
> review to tell whether leaving my_things_ alone was an error or not.
>
> Forbidding (A) means that people can't write such code, but there are times
> where we do want to work with a SCM like a ly_scm_list, including operations
> that change the head SCM.
>
> as_ly_scm_list casts a SCM to a ly_scm_list. There is no separate head
> because there is no new ly_scm_list, just a reinterpretation of the given SCM.
>
> auto& x = as_ly_scm_list (my_things_); // C.
> …
> x.clear (); // D. clears my_things_
>
> Hope that helps.
Thanks, that helps.
Now, the next question. Suppose I just want to iterate over the list, nothing
else. (This is actually the only case I have found myself in.) Suppose I get
this list from an lvalue. The compiler won’t let me use ly_scm_list (lvalue).
Is there a way to use ly_scm_list nevertheless and not as_ly_scm_list in order
not to put the reader in a mode where they look for in-place modifications of
the SCM lvalue?
(Maybe that’s related to std::move? I can’t test what it does, as I am in
vacation, and I never understood all the rules around lvalues, rvalues,
xvalues, prvalues etc)