Thread overview
How to retrieve template parameters
Nov 21, 2005
BCS
Nov 21, 2005
Sean Kelly
Nov 21, 2005
BCS
Nov 21, 2005
Sean Kelly
Nov 22, 2005
BCS
Nov 25, 2005
Thomas Kuehne
Nov 26, 2005
BCS
November 21, 2005
How can you get the specialization parameters for a template from outside the template? This is what I have tried.

template test(int i)
{
struct j {int k;};
}

int main()
{
alias test!(1).j t;
alias test!(2).j u;

printf("%d\n", t.i);
printf("%d\n", u.i);
return 0;
}

The point of what I am trying is to do things like this:

alias test!(t.i + u.i) v;

If what I am trying to do is not possible or is rather convoluted, I would like to propose that a simple way be provided to do this. Such as allow access to them as properties.



November 21, 2005
BCS wrote:
> How can you get the specialization parameters for a template from outside the
> template? This is what I have tried.

You can't.  This is why C++ defines a set of standard typedefs that must be implemented by containers and such.


Sean
November 21, 2005
In article <dltjb9$l0q$1@digitaldaemon.com>, Sean Kelly says...
>
>BCS wrote:
>> How can you get the specialization parameters for a template from outside the template? This is what I have tried.
>
>You can't.  This is why C++ defines a set of standard typedefs that must be implemented by containers and such.
>
>
>Sean

What, if any, problem arise with a syntax allowing that. Just as a starting point, how about this:

template t(int i, T)
{
T fn(){...}
}

int t_i = t!(0,int).i;   // sets t_i to 0;
t!(0,int).T      t_T;    // t_T is of type int
t!(1,real).fn.T  t_fn_T; // t_fn_T is of type real



November 21, 2005
BCS wrote:
> In article <dltjb9$l0q$1@digitaldaemon.com>, Sean Kelly says...
>> BCS wrote:
>>> How can you get the specialization parameters for a template from outside the
>>> template? This is what I have tried.
>> You can't.  This is why C++ defines a set of standard typedefs that must be implemented by containers and such.
> 
> What, if any, problem arise with a syntax allowing that. Just as a starting
> point, how about this:
> 
> template t(int i, T)
> {
> T fn(){...}
> }
> 
> int t_i = t!(0,int).i;   // sets t_i to 0;

I'm not sure I like it, as it introduces symbols into the interface that might be implementation details.  I'd rather stick to the manual method:

class C(int i) {
    const int count = i;
}

const int c = C!(1).count;



Sean
November 22, 2005
In article <dltlpm$n12$1@digitaldaemon.com>, Sean Kelly says...
>
>BCS wrote:
>> In article <dltjb9$l0q$1@digitaldaemon.com>, Sean Kelly says...
>>> BCS wrote:
>>>> How can you get the specialization parameters for a template from outside the template? This is what I have tried.
>>> You can't.  This is why C++ defines a set of standard typedefs that must be implemented by containers and such.
>> 
>> What, if any, problem arise with a syntax allowing that. Just as a starting point, how about this:
>> 
>> template t(int i, T)
>> {
>> T fn(){...}
>> }
>> 
>> int t_i = t!(0,int).i;   // sets t_i to 0;
>
>I'm not sure I like it, as it introduces symbols into the interface that might be implementation details.  I'd rather stick to the manual method:
>
>class C(int i) {
>     const int count = i;
>}
>
>const int c = C!(1).count;
>
>
>
>Sean

ok, then how about public/package/private or somthing like that. Further more how can the above be extended to somthing like:

template fn(int I)
{
void fn(){...}
const int count = I
}

alias fn!(1).fn() hidden;

hidden.count // error: can't get that from this



November 25, 2005
BCS schrieb am 2005-11-22:
> In article <dltlpm$n12$1@digitaldaemon.com>, Sean Kelly says...
>>
>>BCS wrote:
>>> In article <dltjb9$l0q$1@digitaldaemon.com>, Sean Kelly says...
>>>> BCS wrote:
>>>>> How can you get the specialization parameters for a template from outside the template? This is what I have tried.
>>>> You can't.  This is why C++ defines a set of standard typedefs that must be implemented by containers and such.
>>> 
>>> What, if any, problem arise with a syntax allowing that. Just as a starting point, how about this:
>>> 
>>> template t(int i, T)
>>> {
>>> T fn(){...}
>>> }
>>> 
>>> int t_i = t!(0,int).i;   // sets t_i to 0;
>>
>>I'm not sure I like it, as it introduces symbols into the interface that might be implementation details.  I'd rather stick to the manual method:
>>
>>class C(int i) {
>>     const int count = i;
>>}
>>
>>const int c = C!(1).count;
>>
>>
>>
>>Sean
>
> ok, then how about public/package/private or somthing like that. Further more how can the above be extended to somthing like:
>
> template fn(int I)
> {
> void fn(){...}
> const int count = I
> }
>
> alias fn!(1).fn() hidden;
>
> hidden.count // error: can't get that from this


| import std.stdio;
|
| template fn(int I)
| {
|     void fn(){...}
|     const int count = I
| }
| alias fn!(1) hidden;
|
| int main()
| {
|     writefln("%s", hidden.count);
|     return 0;
| }

Thomas


November 26, 2005
No good. What I'm looking for must work on an alias for a template member. e.g.

alias fn!(1).fn() hidden;
hidden()     // this must be a function
hidden.count // this must be a const int

The real purpose of this would be something like this

template t(int i)
{
typedef real t;
}


template math(T, U)
{
// abusing the syntax to say:
// T & U must be from template t but I don't care what i is.
static assert(is(T == t!(*).t) && is(U == t!(*).t);

t!(T.i + U.i).t opMul(T a, U b) { return a*b; }
t!(T.i - U.i).t opDiv(T a, U b) { return a/b; }

t!(T.i).t opAdd(T a, T b) { return a+b; }
t!(T.i).t opSub(T a, T b) { return a-b; }
}

In article <u6tk53-rdd.ln1@birke.kuehne.cn>, Thomas Kuehne says...
>
>BCS schrieb am 2005-11-22:
>> In article <dltlpm$n12$1@digitaldaemon.com>, Sean Kelly says...
>>>
.....
>>
>> ok, then how about public/package/private or somthing like that. Further more how can the above be extended to somthing like:
>>
>> template fn(int I)
>> {
>> void fn(){...}
>> const int count = I
>> }
>>
>> alias fn!(1).fn() hidden;
>>
>> hidden.count // error: can't get that from this
>
>
>| import std.stdio;
>|
>| template fn(int I)
>| {
>|     void fn(){...}
>|     const int count = I
>| }
>| alias fn!(1) hidden;
>|
>| int main()
>| {
>|     writefln("%s", hidden.count);
>|     return 0;
>| }
>
>Thomas
>