Jump to page: 1 2
Thread overview
template ref parameter
Nov 21, 2012
Jack Applegame
Nov 21, 2012
Jonathan M Davis
Nov 21, 2012
Jack Applegame
Nov 21, 2012
bearophile
Nov 21, 2012
Jack Applegame
Nov 21, 2012
Jonathan M Davis
Nov 21, 2012
Jack Applegame
Nov 21, 2012
Jonathan M Davis
Nov 21, 2012
Jack Applegame
Nov 21, 2012
Regan Heath
Nov 21, 2012
Regan Heath
Nov 21, 2012
Regan Heath
Nov 21, 2012
Jack Applegame
Nov 21, 2012
Jack Applegame
Nov 21, 2012
Jack Applegame
Nov 21, 2012
Andrej Mitrovic
November 21, 2012
Why this could not compile?

struct Foo(T) {}
Foo!(ref int) foo;

Output:

Error: expression expected, not 'ref'
Error: found 'int' when expecting ')' following template argument list
Error: no identifier for declarator Foo!(0)
Error: semicolon expected, not ')'
Error: found ')' instead of statement

November 21, 2012
On Wednesday, November 21, 2012 11:55:53 Jack Applegame wrote:
> Why this could not compile?
> 
> struct Foo(T) {}
> Foo!(ref int) foo;
> 
> Output:
> 
> Error: expression expected, not 'ref'
> Error: found 'int' when expecting ')' following template argument
> list
> Error: no identifier for declarator Foo!(0)
> Error: semicolon expected, not ')'
> Error: found ')' instead of statement

ref is not part of a type. It's only a storage class, and it's only legal on function parameters, return types, and foreach's loop variables. It's not legal on local variables or member variables or anything of the sort. So, ref int for a template argument makes no sense whatsoever.

- Jonathan M Davis
November 21, 2012
But sometimes ref becomes a part of type. For example "void delegate(ref int)" and "void delegate(int)" are different types. Is it possible to cast from one to another safely? For example:

void foo(ref int);
void function(int) fp;
fp = cast(typeof(fp)) foo; /// is it safe???
November 21, 2012
How to implement functor-like objects if ref is storage class?

void foo(ref int a) { a--; }
struct functor(A...) {
  void function(A) functor;
}
functor!int f;    // functor!(ref int) - wrong
f.functor = &foo; // Error: cannot implicitly convert expression (& foo) of type void function(ref int a) to void function(int)

November 21, 2012
Jack Applegame:

A brony? :-)

> But sometimes ref becomes a part of type. For example "void delegate(ref int)" and "void delegate(int)" are different types. Is it possible to cast from one to another safely? For example:
>
> void foo(ref int);
> void function(int) fp;
> fp = cast(typeof(fp)) foo; /// is it safe???

In one case the function expects a pointer and in one case it expects a int value. The assembly code of the two functions is different and does different things.

Bye,
bearophile
November 21, 2012
On Wednesday, 21 November 2012 at 12:05:23 UTC, bearophile wrote:
> In one case the function expects a pointer and in one case it expects a int value. The assembly code of the two functions is different and does different things.
Of course. And it is mean that ref indeed is a part of type.

November 21, 2012
On Wednesday, November 21, 2012 12:57:57 Jack Applegame wrote:
> But sometimes ref becomes a part of type. For example "void
> delegate(ref int)" and "void delegate(int)" are different types.
> Is it possible to cast from one to another safely? For example:

That's because ref is being used on a function parameter. That gives the function a different type, but you can't just use ref int by itself. As I said, it's only applicable to function parameters, return types, and foreach loop variables.

> void foo(ref int);
> void function(int) fp;
> fp = cast(typeof(fp)) foo; /// is it safe???

No, it's not safe.

- Jonathan M Davis
November 21, 2012
On Wednesday, November 21, 2012 13:09:05 Jack Applegame wrote:
> On Wednesday, 21 November 2012 at 12:05:23 UTC, bearophile wrote:
> > In one case the function expects a pointer and in one case it expects a int value. The assembly code of the two functions is different and does different things.
> 
> Of course. And it is mean that ref indeed is a part of type.

storage classes on function parameters affect the type of the function, but they do not affect the type of the parameter. If a function has a parameter named foo, then typeof(foo) is going to be the same whether ref is on it or not.

- Jonathan M Davis
November 21, 2012
On Wed, 21 Nov 2012 12:02:45 -0000, Jack Applegame <japplegame@gmail.com> wrote:

> void foo(ref int a) { a--; }
> struct functor(A...) {
>   void function(A) functor;
> }
> functor!int f;    // functor!(ref int) - wrong
> f.functor = &foo; // Error: cannot implicitly convert expression (& foo) of type void function(ref int a) to void function(int)

Hmm.. well I got past your initial problem, but I have a new one..

alias void function(ref int) funcType;

void foo(ref int a) { a--; }

struct functor(A...) {
  A func;
}
void main()
{
  functor!funcType f;
  f.func = &foo; //Error: f.func is not an lvalue
}


-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/
November 21, 2012
On Wed, 21 Nov 2012 12:30:08 -0000, Regan Heath <regan@netmail.co.nz> wrote:

> On Wed, 21 Nov 2012 12:02:45 -0000, Jack Applegame <japplegame@gmail.com> wrote:
>
>> void foo(ref int a) { a--; }
>> struct functor(A...) {
>>   void function(A) functor;
>> }
>> functor!int f;    // functor!(ref int) - wrong
>> f.functor = &foo; // Error: cannot implicitly convert expression (& foo) of type void function(ref int a) to void function(int)
>
> Hmm.. well I got past your initial problem, but I have a new one..
>
> alias void function(ref int) funcType;
>
> void foo(ref int a) { a--; }
>
> struct functor(A...) {
>    A func;
> }
> void main()
> {
>    functor!funcType f;
>    f.func = &foo; //Error: f.func is not an lvalue
> }

Just realised what is happening here. "f.func = " is trying to call the function, and assign &foo to the "void" result.

R

-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/
« First   ‹ Prev
1 2