www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to retrieve template parameters

reply BCS <BCS_member pathlink.com> writes:
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.
Nov 21 2005
parent reply Sean Kelly <sean f4.ca> writes:
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
Nov 21 2005
parent reply BCS <BCS_member pathlink.com> writes:
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
Nov 21 2005
parent reply Sean Kelly <sean f4.ca> writes:
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
Nov 21 2005
parent reply BCS <BCS_member pathlink.com> writes:
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
Nov 21 2005
parent reply Thomas Kuehne <thomas-dloop kuehne.cn> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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 -----BEGIN PGP SIGNATURE----- iD8DBQFDh8W+3w+/yD4P9tIRAixpAKC2SxssMjy2BuwShZ+fJtVApr98cgCfQ+C+ TM7c2K2EAzE/usuqq/zOzJc= =NjvR -----END PGP SIGNATURE-----
Nov 25 2005
parent BCS <BCS_member pathlink.com> writes:
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
Nov 26 2005