Thread overview
Template type parameters with their own type parameters
Mar 05, 2011
Peter Lundgren
Mar 05, 2011
spir
Mar 06, 2011
Peter Lundgren
March 05, 2011
I have a function that I think should look something like this:

MyStruct!T myFunc(T)(MyStruct!T x, ...) {
	...
	return MyStruct!T(...);
}

and the closest I can get to is:

T myFunc(T)(T x, ...) {
	...
	return T(...);
}

which works, but doesn't make clear the intended use and gets in the way of overloading. How can I express the intent of the first version.
March 05, 2011
On 03/05/2011 04:02 AM, Peter Lundgren wrote:
> I have a function that I think should look something like this:
>
> MyStruct!T myFunc(T)(MyStruct!T x, ...) {
> 	...
> 	return MyStruct!T(...);
> }
>
> and the closest I can get to is:
>
> T myFunc(T)(T x, ...) {
> 	...
> 	return T(...);
> }
>
> which works, but doesn't make clear the intended use and gets in the way of
> overloading. How can I express the intent of the first version.

Maybe I do not exactly understand your problem; anyway, the following runs fine by me:

struct S (T) {
    T v;
}

S!T inc (T) (S!T s) {
    return S!T(s.v + 1);
}

unittest {
    auto s1 = S!int(1);
    auto s2 = inc(s1);
    assert ( s2.v == 2 );
}

Could you provide (1) context (2) example (3) errors?

Denis
-- 
_________________
vita es estrany
spir.wikidot.com

March 06, 2011
== Quote from spir (denis.spir@gmail.com)'s article
> On 03/05/2011 04:02 AM, Peter Lundgren wrote:
> > I have a function that I think should look something like this:
> >
> > MyStruct!T myFunc(T)(MyStruct!T x, ...) {
> > 	...
> > 	return MyStruct!T(...);
> > }
> >
> > and the closest I can get to is:
> >
> > T myFunc(T)(T x, ...) {
> > 	...
> > 	return T(...);
> > }
> >
> > which works, but doesn't make clear the intended use and gets in the way of overloading. How can I express the intent of the first version.
> Maybe I do not exactly understand your problem; anyway, the following runs fine
> by me:
> struct S (T) {
>      T v;
> }
> S!T inc (T) (S!T s) {
>      return S!T(s.v + 1);
> }
> unittest {
>      auto s1 = S!int(1);
>      auto s2 = inc(s1);
>      assert ( s2.v == 2 );
> }
> Could you provide (1) context (2) example (3) errors?
> Denis

Thanks for the help. I'd convinced myself that it didn't work and missed the actual problem. I was mixing template type and template value parameters. What I really wanted was this:

MyStruct!v myFunc(string v)(MyStruct!v x, ...) {
 ...
 return MyStruct!v(...);
}