digitalmars.D - what means... auto ref Args args?
- Dave Jones (4/4) Oct 18 2017 Poking around in the source code for emplace and I noticed...
- Dave Jones (2/6) Oct 18 2017 sorry meant to put this in learn
- Moritz Maxeiner (5/9) Oct 18 2017 It means that any argument (that is an element of args) will be
- Dave Jones (4/15) Oct 18 2017 So it's just to make sure any "ref" in the eventual call to Ts
- Jonathan M Davis (8/25) Oct 18 2017 That's likely the main reason in this case, since losing the ref-ness co...
- Dave Jones (3/12) Oct 19 2017 Makes sense, thanks.
Poking around in the source code for emplace and I noticed... T* emplace(T, Args...)(T* chunk, auto ref Args args) what does the "auto ref" do in this situiation? Cant seem to find any explanation in the docs.
Oct 18 2017
On Wednesday, 18 October 2017 at 21:38:41 UTC, Dave Jones wrote:Poking around in the source code for emplace and I noticed... T* emplace(T, Args...)(T* chunk, auto ref Args args) what does the "auto ref" do in this situiation? Cant seem to find any explanation in the docs.sorry meant to put this in learn
Oct 18 2017
On Wednesday, 18 October 2017 at 21:38:41 UTC, Dave Jones wrote:Poking around in the source code for emplace and I noticed... T* emplace(T, Args...)(T* chunk, auto ref Args args) what does the "auto ref" do in this situiation? Cant seem to find any explanation in the docs.It means that any argument (that is an element of args) will be passed by reference if and only if it's an lvalue (has a memory address that can be taken) (it'll be passed by value otherwise). https://dlang.org/spec/template.html#auto-ref-parameters
Oct 18 2017
On Wednesday, 18 October 2017 at 22:16:32 UTC, Moritz Maxeiner wrote:On Wednesday, 18 October 2017 at 21:38:41 UTC, Dave Jones wrote:So it's just to make sure any "ref" in the eventual call to Ts constructor is also reflected in the call to emplace?Poking around in the source code for emplace and I noticed... T* emplace(T, Args...)(T* chunk, auto ref Args args) what does the "auto ref" do in this situiation? Cant seem to find any explanation in the docs.It means that any argument (that is an element of args) will be passed by reference if and only if it's an lvalue (has a memory address that can be taken) (it'll be passed by value otherwise). https://dlang.org/spec/template.html#auto-ref-parameters
Oct 18 2017
On Thursday, October 19, 2017 00:00:54 Dave Jones via Digitalmars-d wrote:On Wednesday, 18 October 2017 at 22:16:32 UTC, Moritz Maxeiner wrote:That's likely the main reason in this case, since losing the ref-ness could definitely cause issues with some constructors, but auto ref is frequently used simply to avoid copying lvalues while not requiring lvalues (since if ref is used, the argument must be an lvalue, and if ref is not used, the argument will be copied if it's an lvalue; it will be moved if it's an rvalue). D never binds rvalues to ref like C++ does with const T&. - Jonathan M DavisOn Wednesday, 18 October 2017 at 21:38:41 UTC, Dave Jones wrote:So it's just to make sure any "ref" in the eventual call to Ts constructor is also reflected in the call to emplace?Poking around in the source code for emplace and I noticed... T* emplace(T, Args...)(T* chunk, auto ref Args args) what does the "auto ref" do in this situiation? Cant seem to find any explanation in the docs.It means that any argument (that is an element of args) will be passed by reference if and only if it's an lvalue (has a memory address that can be taken) (it'll be passed by value otherwise). https://dlang.org/spec/template.html#auto-ref-parameters
Oct 18 2017
On Thursday, 19 October 2017 at 01:05:28 UTC, Jonathan M Davis wrote:On Thursday, October 19, 2017 00:00:54 Dave Jones viaThat's likely the main reason in this case, since losing the ref-ness could definitely cause issues with some constructors, but auto ref is frequently used simply to avoid copying lvalues while not requiring lvalues (since if ref is used, the argument must be an lvalue, and if ref is not used, the argument will be copied if it's an lvalue; it will be moved if it's an rvalue). D never binds rvalues to ref like C++ does with const T&. - Jonathan M DavisMakes sense, thanks.
Oct 19 2017