Advanced templating 
Author Message
 Advanced templating

* Warning *

If you're new to templating, this may cause your head to explode.

If you get latched onto these problems easily, this may consume

     your freetime for the next several days.

Given a class with several functions....

class Foo

{

public:

    void SetX(int x);

    void SetY(int y);

Quote:
};

How could one make a container class.....

template <class item>

class MyContainer

{

    // Implementation unknown

Quote:
};

such that you could create an instance like so...

MyContainer <Foo> theContainer;

then make function call to all items in the container like so

theContainer.SetX(7);    // Should call SetX for each item

theContainer.SetY(42);  // Should call SetY for each item

Just curious - not urgent....take your time and savor.

Any solution is acceptable.  (Example: I got the closest to a solution using
macros combined with templates.)

Full item encapsulation is preferred.

   (The container's internal item storage should be private or protected)

One line instantiation, Zero line definition is preferred.

    (Should not have to define anything to make it work for a new item
type.)

--
Kanon

Of course it's nuts.  Otherwise it wouldn't be worth tackling.



Tue, 20 Dec 2005 03:55:43 GMT  
 Advanced templating


Quote:
>Given a class with several functions....
>class Foo
>{
>public:
>    void SetX(int x);
>    void SetY(int y);
>};
>How could one make a container class.....
>template <class item>
>class MyContainer
>{
>    // Implementation unknown
>};
>such that you could create an instance like so...
>MyContainer <Foo> theContainer;
>then make function call to all items in the container like so
>theContainer.SetX(7);    // Should call SetX for each item
>theContainer.SetY(42);  // Should call SetY for each item

In order to inject Foo's interface into that of MyContainer, it would
have to inherit from Foo, but that's no good since you don't want to
call SetY on the container, but on each contained element.

Quote:
>Just curious - not urgent....take your time and savor.

>Any solution is acceptable.  (Example: I got the closest to a solution using
>macros combined with templates.)

>Full item encapsulation is preferred.

>   (The container's internal item storage should be private or protected)

>One line instantiation, Zero line definition is preferred.

>    (Should not have to define anything to make it work for a new item
>type.)

Well, within the constraints it isn't possible without making
Container have a function with every possible function name up to a
certain length (which would be theoretically possible given sufficient
amount of memory). e.g.

void a()
{
        //for loop calling a on each element

Quote:
}

template <class T>
void a(T t)
{
        //for loop calling a(t) on each element
Quote:
}

//more a overloads

void b()
{

Quote:
}

//...
void z()
//...
void a1()
//.....
template <class T1, class T2, class T3>
void aa1bczAbdD(T1 t1, T2 t2, T3 t3)

In the absense of that (!), then what's wrong with standard algorithm
techniques, using iterators?

for_each(c.begin(), c.end(), bind(&Foo::setX, _1, 7));

If you really had to, you could add for_each to the interface of
MyContainer:

c.for_each(bind(&Foo::setX, _1, 7));

Tom



Sat, 24 Dec 2005 23:21:51 GMT  
 Advanced templating
The original problem concerned me making an object (templated container
class) for an intermediate level programming club.  The object was to be
simple to use (excludes most stl calls) and semi-intelligent toward it's
items.  Users could construct this object for any class they wrote, but that
class had to implement certain functions.  I remember they had to implement,
bool Test(int iTestId).  The users could then Add() members to the
container, and execute functions on all items who answered true to Test().

It's been some months now.  I never did find an implementation that was
powerful and simple.  I still, however, consider it a worthy challenge and
thus give it thought from time to time.

-Kanon


Quote:


> >Given a class with several functions....
> >class Foo
> >{
> >public:
> >    void SetX(int x);
> >    void SetY(int y);
> >};
> >How could one make a container class.....
> >template <class item>
> >class MyContainer
> >{
> >    // Implementation unknown
> >};
> >such that you could create an instance like so...
> >MyContainer <Foo> theContainer;
> >then make function call to all items in the container like so
> >theContainer.SetX(7);    // Should call SetX for each item
> >theContainer.SetY(42);  // Should call SetY for each item

> In order to inject Foo's interface into that of MyContainer, it would
> have to inherit from Foo, but that's no good since you don't want to
> call SetY on the container, but on each contained element.

> >Just curious - not urgent....take your time and savor.

> >Any solution is acceptable.  (Example: I got the closest to a solution
using
> >macros combined with templates.)

> >Full item encapsulation is preferred.

> >   (The container's internal item storage should be private or protected)

> >One line instantiation, Zero line definition is preferred.

> >    (Should not have to define anything to make it work for a new item
> >type.)

> Well, within the constraints it isn't possible without making
> Container have a function with every possible function name up to a
> certain length (which would be theoretically possible given sufficient
> amount of memory). e.g.

> void a()
> {
> //for loop calling a on each element
> }

> template <class T>
> void a(T t)
> {
> //for loop calling a(t) on each element
> }
> //more a overloads

> void b()
> {
> }
> //...
> void z()
> //...
> void a1()
> //.....
> template <class T1, class T2, class T3>
> void aa1bczAbdD(T1 t1, T2 t2, T3 t3)

> In the absense of that (!), then what's wrong with standard algorithm
> techniques, using iterators?

> for_each(c.begin(), c.end(), bind(&Foo::setX, _1, 7));

> If you really had to, you could add for_each to the interface of
> MyContainer:

> c.for_each(bind(&Foo::setX, _1, 7));

> Tom



Sun, 25 Dec 2005 21:25:11 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Simplest page templating solution?

2. Templating functions on integer values

3. Advanced debugging in code (Debugging API, breakpoints)

4. C++ Streams using COM and Unlocked Critical Section (Advanced)

5. Book recommendation (intermediate/advanced)

6. Refresher books for intermediate to advanced C++ devs?

7. Advanced C programming books?

8. kind of advanced tutorials

9. Advanced math and matrix functions in C/C++

10. Intermediate to Advanced Books

11. Advanced advice wanted (warning: long)

12. Advanced Finite Automaton as a simple HTML formatter [Crazy Example]

 

 
Powered by phpBB® Forum Software