Generic Functor

Discussion in 'C++' started by Luc Claustres, Dec 3, 2004.

  1. I have a generic container such as:

    template<class T>
    class Container
    {
    // some data structure that store elements of type T
    }

    I use this container in a hierarchical manner,
    that is elements can be themselves containers:

    Container<Container<float>> lMyContainer;

    What I want to do is to call a method on the
    container that has to be recursively called
    on its elements if they are not simple type
    but container themselves.
    The best idea I've got for the moment is to
    declare a template "operator" called by the
    container and specialized for simple types:

    template<class T>
    struct Operator
    {
    static void f(T& element) { element.f(); }
    }

    template<>
    struct Operator
    {
    static void f(float& element) { // Do nothing }
    }

    Then in the container I do:

    template<class T>
    void f()
    {
    // Perform computation on the elements
    ...
    // Then call the method on the elements
    Iterate on each element E
    Operator<T>::f(E);
    }

    I'd like something more elegant and which
    behaviour may be changed dynamically by the user
    at execution time (not at compilation).
    I think this could be done through functors
    but I did not find the solution.

    Thanks in advance.

    Luc Claustres
     
    Luc Claustres, Dec 3, 2004
    #1
    1. Advertising

  2. Luc Claustres

    Chris Theis Guest

    "Luc Claustres" <> wrote in message
    news:...
    > I have a generic container such as:
    >
    > template<class T>
    > class Container
    > {
    > // some data structure that store elements of type T
    > }
    >
    > I use this container in a hierarchical manner,
    > that is elements can be themselves containers:
    >
    > Container<Container<float>> lMyContainer;
    >
    > What I want to do is to call a method on the
    > container that has to be recursively called
    > on its elements if they are not simple type
    > but container themselves.
    > The best idea I've got for the moment is to
    > declare a template "operator" called by the
    > container and specialized for simple types:
    >
    > template<class T>
    > struct Operator
    > {
    > static void f(T& element) { element.f(); }
    > }
    >
    > template<>
    > struct Operator
    > {
    > static void f(float& element) { // Do nothing }
    > }
    >
    > Then in the container I do:
    >
    > template<class T>
    > void f()
    > {
    > // Perform computation on the elements
    > ...
    > // Then call the method on the elements
    > Iterate on each element E
    > Operator<T>::f(E);
    > }
    >
    > I'd like something more elegant and which
    > behaviour may be changed dynamically by the user
    > at execution time (not at compilation).
    > I think this could be done through functors
    > but I did not find the solution.
    >
    > Thanks in advance.
    >
    > Luc Claustres


    The problem with specialization is that you might have to cover a rather
    wide range of possibilities. A better solution would be to think about a
    functor which differentiates between types and containers with these types.
    For example:

    template<typename T>
    class CFunctor {
    public:
    void operator() ( T& Val ) {
    // treat the type }

    void operator()( CMyContainer<T>& Cont ) {
    // treat the container
    }
    };

    HTH
    Chris
     
    Chris Theis, Dec 3, 2004
    #2
    1. Advertising

  3. Luc Claustres

    Chris Theis Guest

    "Luc Claustres" <> wrote in message
    news:...
    > I have a generic container such as:
    >
    > template<class T>
    > class Container
    > {
    > // some data structure that store elements of type T
    > }
    >
    > I use this container in a hierarchical manner,
    > that is elements can be themselves containers:
    >
    > Container<Container<float>> lMyContainer;
    >
    > What I want to do is to call a method on the
    > container that has to be recursively called
    > on its elements if they are not simple type
    > but container themselves.
    > The best idea I've got for the moment is to
    > declare a template "operator" called by the
    > container and specialized for simple types:
    >
    > template<class T>
    > struct Operator
    > {
    > static void f(T& element) { element.f(); }
    > }
    >
    > template<>
    > struct Operator
    > {
    > static void f(float& element) { // Do nothing }
    > }
    >
    > Then in the container I do:
    >
    > template<class T>
    > void f()
    > {
    > // Perform computation on the elements
    > ...
    > // Then call the method on the elements
    > Iterate on each element E
    > Operator<T>::f(E);
    > }
    >
    > I'd like something more elegant and which
    > behaviour may be changed dynamically by the user
    > at execution time (not at compilation).
    > I think this could be done through functors
    > but I did not find the solution.
    >
    > Thanks in advance.
    >
    > Luc Claustres


    The problem with specialization is that you might have to cover a rather
    wide range of possibilities. A better solution would be to think about a
    functor which differentiates between types and containers with these types.
    For example:

    template<typename T>
    class CFunctor {
    public:
    void operator() ( T& Val ) {
    // treat the type }

    void operator()( CMyContainer<T>& Cont ) {
    // treat the container
    }
    };

    HTH
    Chris
     
    Chris Theis, Dec 3, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Murat Tasan
    Replies:
    1
    Views:
    8,051
    Chaitanya
    Feb 3, 2009
  2. Chandra Shekhar Kumar

    functor object in template class

    Chandra Shekhar Kumar, Jun 24, 2003, in forum: C++
    Replies:
    5
    Views:
    1,275
    porschberg
    Jun 26, 2003
  3. Sean
    Replies:
    2
    Views:
    472
  4. Replies:
    2
    Views:
    437
  5. minlearn
    Replies:
    2
    Views:
    457
    red floyd
    Mar 13, 2009
Loading...

Share This Page