Jump to page: 1 2 3
Thread overview
still confused about call by reference
Oct 29, 2007
Hoenir
Oct 29, 2007
Derek Parnell
Oct 30, 2007
Hoenir
Oct 30, 2007
Hoenir
Oct 30, 2007
Nathan Reed
Oct 30, 2007
Hoenir
Oct 30, 2007
Nathan Reed
Oct 31, 2007
Saaa
Oct 31, 2007
Bill Baxter
Oct 31, 2007
Saaa
Oct 31, 2007
Hoenir
Nov 01, 2007
Hoenir
Oct 29, 2007
Nathan Reed
Oct 29, 2007
Hoenir
Oct 29, 2007
Nathan Reed
Oct 31, 2007
Hoenir
Nov 01, 2007
Nathan Reed
Nov 01, 2007
Hoenir
Nov 01, 2007
Bill Baxter
Nov 01, 2007
Hoenir
Nov 02, 2007
Bill Baxter
Oct 29, 2007
Bill Baxter
October 29, 2007
I'm still a bit confused about call by reference. When do I have to explicitly use the & operator and when to use in, out and inout?
I want to convert my C++ vector struct to D:

struct vec3
{
	vec3 operator+(const vec3& v) const
	{return vec3(x+v.x, y+v.y, z+v.z);}
	vec3 operator-(const vec3& v) const
	{return vec3(x-v.x, y-v.y, z-v.z);}

I'm also wondering about how to handle the constness.
Thanks in advance for any help :)
October 29, 2007
On Mon, 29 Oct 2007 04:32:44 +0100, Hoenir wrote:

> I'm still a bit confused about call by reference. When do I have to
> explicitly use the & operator and when to use in, out and inout?
> I want to convert my C++ vector struct to D:
> 
> struct vec3
> {
> 	vec3 operator+(const vec3& v) const
> 	{return vec3(x+v.x, y+v.y, z+v.z);}
> 	vec3 operator-(const vec3& v) const
> 	{return vec3(x-v.x, y-v.y, z-v.z);}
> 
> I'm also wondering about how to handle the constness. Thanks in advance for any help :)

I'm no C++ user so I'm guessing a bit with the C++ syntax, but I'd code something like ...

module vecs;
struct vec3(VT)
{
    private VT x,y,z;

    vec3 opAdd(const ref vec3 v)
    {

        vec3 t;

        t.x = x + v.x;
        t.y = y + v.y;
        t.z = z + v.z;
        return t;

    }

    vec3 opSub(const ref vec3 v)
    {
        vec3 t;

        t.x = x - v.x;
        t.y = y - v.y;
        t.z = z - v.z;

        return t;
    }

    void opCall(T,U,V)(const T a, const U b, const V c)
    {
        x = cast(VT)a;
        y = cast(VT)b;
        z = cast(VT)c;
    }


    private import std.string;
    string toString()
    {
        return std.string.format("[%s; %s; %s]", x,y,z);
    }
}

**** EXCEPT **** that 'const ref' crashes the compiler (see Bugzilla 1319)

  http://d.puremagic.com/issues/show_bug.cgi?id=1319


-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
29/10/2007 2:58:45 PM
October 29, 2007
Hoenir wrote:
> I'm still a bit confused about call by reference. When do I have to explicitly use the & operator and when to use in, out and inout?
> I want to convert my C++ vector struct to D:
> 
> struct vec3
> {
>     vec3 operator+(const vec3& v) const
>     {return vec3(x+v.x, y+v.y, z+v.z);}
>     vec3 operator-(const vec3& v) const
>     {return vec3(x-v.x, y-v.y, z-v.z);}
> 
> I'm also wondering about how to handle the constness.
> Thanks in advance for any help :)

In C++, const references are used to signal the data is not changed by the function, and is passed by reference purely for efficiency's sake. I believe the correct D equivalent is 'in'.  The D spec states that 'in' is equivalent to 'final const scope', which means writing to the parameter is prevented, as in C++.

The spec does not say whether 'in' results in pass-by-reference or not, but in this case, I believe that is a decision that should be made by the compiler, not the programmer.

C++ non-const references are used to signal out-parameters, so the D equivalent would be out or inout, depending on whether your function wants to use the value that is passed in or not - 'out' parameters are initialized to their type's default value when the function begins, so any value that was there when the function was called gets clobbered, while 'inout' parameters let you read the original value.

Honestly, I'm not entirely sure what D's 'ref' parameter-storage class is for, since AFAICT all the uses of pass-by-reference are covered by in, out, and inout.

Thanks,
Nathan Reed
October 29, 2007
Nathan Reed schrieb:
> Hoenir wrote:
>> I'm still a bit confused about call by reference. When do I have to explicitly use the & operator and when to use in, out and inout?
>> I want to convert my C++ vector struct to D:
>>
>> struct vec3
>> {
>>     vec3 operator+(const vec3& v) const
>>     {return vec3(x+v.x, y+v.y, z+v.z);}
>>     vec3 operator-(const vec3& v) const
>>     {return vec3(x-v.x, y-v.y, z-v.z);}
>>
>> I'm also wondering about how to handle the constness.
>> Thanks in advance for any help :)
> 
> In C++, const references are used to signal the data is not changed by the function, and is passed by reference purely for efficiency's sake. I believe the correct D equivalent is 'in'.  The D spec states that 'in' is equivalent to 'final const scope', which means writing to the parameter is prevented, as in C++.
> 
> The spec does not say whether 'in' results in pass-by-reference or not, but in this case, I believe that is a decision that should be made by the compiler, not the programmer.
> 
> C++ non-const references are used to signal out-parameters, so the D equivalent would be out or inout, depending on whether your function wants to use the value that is passed in or not - 'out' parameters are initialized to their type's default value when the function begins, so any value that was there when the function was called gets clobbered, while 'inout' parameters let you read the original value.
> 
> Honestly, I'm not entirely sure what D's 'ref' parameter-storage class is for, since AFAICT all the uses of pass-by-reference are covered by in, out, and inout.
> 
> Thanks,
> Nathan Reed
I think I read somewhere, objects are automatically passed by reference and if you add in or out it means the pointer rather than the data.
October 29, 2007
"Nathan Reed" <nathaniel.reed@gmail.com> wrote in message news:fg40ac$2fuo$1@digitalmars.com...
>
> Honestly, I'm not entirely sure what D's 'ref' parameter-storage class is for, since AFAICT all the uses of pass-by-reference are covered by in, out, and inout.

'ref' is an alias of 'inout' at least for the time being.  It was introduced for forwards compatibility, as it seems likely that we will be getting reference returns, and returning an 'inout' doesn't really make much sense.


October 29, 2007
Hoenir wrote:
> I think I read somewhere, objects are automatically passed by reference and if you add in or out it means the pointer rather than the data.

Object types in D are indeed reference types, so passing these by reference would mean you'd pass a reference to a reference.  (This is useless for 'in' parameters, but allows the expected behavior for 'out' parameters.)  Of course, references are implemented as pointers, but they're semantically distinct from pointers.

Thanks,
Nathan Reed

October 29, 2007
Jarrett Billingsley wrote:
> "Nathan Reed" <nathaniel.reed@gmail.com> wrote in message news:fg40ac$2fuo$1@digitalmars.com...
>> Honestly, I'm not entirely sure what D's 'ref' parameter-storage class is for, since AFAICT all the uses of pass-by-reference are covered by in, out, and inout.
> 
> 'ref' is an alias of 'inout' at least for the time being.  It was introduced for forwards compatibility, as it seems likely that we will be getting reference returns, and returning an 'inout' doesn't really make much sense. 

Also 'const inout' as a parameter doesn't make much sense either.

--bb
October 30, 2007
Derek Parnell schrieb:
>     void opCall(T,U,V)(const T a, const U b, const V c)
>     {
>         x = cast(VT)a;
>         y = cast(VT)b;
>         z = cast(VT)c;
>     }
Thanks for your code. But is there a way to use code like the following:

vec3 opSub(vec3 v) {return vec3(x-v.x, y-v.y, z-v.z);}

It doesn't work with
	vec3 opCall(U,V,W)(U a, V b, W c)
	{
		x = cast(T)a;
		y = cast(T)b;
		z = cast(T)c;
		return *this;
	}
Tells me "type vec3!(real) is not an expression". (typedef double real)
btw is it possible to return pointers?
October 30, 2007
Always wondered why it uses real. It's because I use the following typedef:

typedef vec3!(real) color;
October 30, 2007
Hoenir wrote:
> Derek Parnell schrieb:
>>     void opCall(T,U,V)(const T a, const U b, const V c)
>>     {
>>         x = cast(VT)a;
>>         y = cast(VT)b;
>>         z = cast(VT)c;
>>     }
> Thanks for your code. But is there a way to use code like the following:
> 
> vec3 opSub(vec3 v) {return vec3(x-v.x, y-v.y, z-v.z);}
> 
> It doesn't work with
>     vec3 opCall(U,V,W)(U a, V b, W c)
>     {
>         x = cast(T)a;
>         y = cast(T)b;
>         z = cast(T)c;
>         return *this;
>     }
> Tells me "type vec3!(real) is not an expression". (typedef double real)
> btw is it possible to return pointers?

I think the opCall needs to be declared static.

Thanks,
Nathan Reed
« First   ‹ Prev
1 2 3