digitalmars.D.learn - template template parameter
- Caligo (18/18) Mar 30 2011 I have a struct that looks something like this:
- =?ISO-8859-1?Q?Ali_=C7ehreli?= (27/45) Mar 30 2011 Template template parameter means exactly that: a template parameter is
- Caligo (11/11) Mar 31 2011 I should have been more clear, but my actual question is how do I
- =?ISO-8859-1?Q?Ali_=C7ehreli?= (19/38) Mar 31 2011 With the help of function template parameter deduction, I was able to do...
I have a struct that looks something like this:
struct Box(T, size_t width, size_t height){
alias width width_;
alias height height_;
//do something with a Box of different size
void Fun( B )(B b){
// using width and height of b
B.width_;
B.height_;
}
}
auto b1 = Box!(double, 2, 7)();
auto b2 = Box!(double, 3, 4)();
b1.Fun(b2);
I think the technical name for this is template template parameter.
Using the above I'm able to do what I need to do, but is there a
better way?
and should I use alias or enum?
Mar 30 2011
On 03/30/2011 04:32 PM, Caligo wrote:
I have a struct that looks something like this:
struct Box(T, size_t width, size_t height){
alias width width_;
alias height height_;
//do something with a Box of different size
void Fun( B )(B b){
// using width and height of b
B.width_;
B.height_;
}
}
auto b1 = Box!(double, 2, 7)();
auto b2 = Box!(double, 3, 4)();
b1.Fun(b2);
I think the technical name for this is template template parameter.
Using the above I'm able to do what I need to do, but is there a
better way?
and should I use alias or enum?
Template template parameter means exactly that: a template parameter is
itself a template. See ContainerType below:
struct SomeContainer(T)
{
T[] elements;
}
struct SomeOtherContainer(T)
{
struct List(T)
{}
// e.g. this one may use a linked list
List!T elements;
}
struct Foo(alias ContainerType, T)
{
// Instantiate the 'template template parameter'
ContainerType!T member;
}
void main()
{
Foo!(SomeContainer, double) foo1;
Foo!(SomeOtherContainer, int) foo2;
}
ContainerType is a template parameter of Foo and is itself a template.
And 'alias' seems to work...
Ali
Mar 30 2011
I should have been more clear, but my actual question is how do I
access the parameters of a template parameter. My example works, but
I wanted to know if there is a different and perhaps a better of doing
it.
In your example it would look something like this:
struct SomeContainer(T, int x, int y){ }
struct Foo(alias ContainerType){
// e.g.
int my_x = ContainerType.x * 2;
int my_y = ContainerType.y * 2;
}
Mar 31 2011
On 03/30/2011 04:32 PM, Caligo wrote:I have a struct that looks something like this: struct Box(T, size_t width, size_t height){ alias width width_; alias height height_; //do something with a Box of different size void Fun( B )(B b){ // using width and height of b B.width_; B.height_; } } auto b1 = Box!(double, 2, 7)(); auto b2 = Box!(double, 3, 4)(); b1.Fun(b2); I think the technical name for this is template template parameter. Using the above I'm able to do what I need to do, but is there a better way? and should I use alias or enum?You later also said:how do I access the parameters of a template parameterWith the help of function template parameter deduction, I was able to do it by spelling out the parameters of Fun: import std.stdio; struct Box(T, int width, int height) { void Fun(T2, int width2, int height2)(Box!(T2, width2, height2) b) { writeln(width2, ' ', height2); } } void main() { auto b1 = Box!(double, 2, 7)(); auto b2 = Box!(double, 3, 4)(); b1.Fun(b2); } Ali
Mar 31 2011









Caligo <iteronvexor gmail.com> 