Boost logo

Boost Users :

From: Ames Andreas (Andreas.Ames_at_[hidden])
Date: 2005-08-30 05:01:38


Hi all,

I could use some advice regarding this lengthy code example:

---------- <code> ------------
#include <vector>
#include <map>

#include <boost/function.hpp>
#include <boost/assign.hpp>

struct E1 {};
struct E2 {};

enum State
{
  eS1 = 1,
  eS2
};

typedef boost::function< bool (const E1 &, int) > function0_t;
typedef boost::function< bool (const E2 &, int) > function1_t;

struct Functions
{
// Functions(std::vector< function0_t > af0 , std::vector< function1_t > af1)
// : f0(af0) , f1(af1)
// {
// }
    std::vector< function0_t > f0;
    std::vector< function1_t > f1;
};

struct Handler1
{
    bool operator()(const E1&, int) {return true;}
};

struct Handler1a
{
    bool operator()(const E1&, int) {return true;}
};

struct Handler2
{
    bool operator()(const E2&, int) {return true;}
};

typedef std::map< State, Functions > InitDesc;

using namespace boost::assign;

int main()
{
    // [1] this works just fine
    Functions f1 = {
          list_of(Handler1()),
          list_of(Handler2())};
    // [2] this doesn't work cause Handler1 and Handler1a have different types
    Functions f2 = {
          list_of(Handler1())(Handler1a()),
          list_of(Handler2())};
    // [3] don't know how to do this
    InitDesc desc = map_list_of(eS1, {list_of(Handler1()), list_of(Handler2())})
        (eS2, {list_of(Handler1a()), list_of(Handler2())});
}
---------- </code> ------------

The places marked with [1], [2] and [3] are of special interest for
me.

I like [1] very much thanks to what bost::assign offers me.
Unfortunately I can get [2] only to work, if I explicitly create
instances of function0_t from Handler1 and Handler1a. Are there more
elegant ways to make this work?

[3] is obviously broken, but I don't know how to come up with a
similarly compact way to initialise an InitDesc instance. I can
uncomment Funtions' constructor above and call it explicitely with
what I've written into curly braces but that unfortunately breaks [1]
(because Functions is no longer aggregate then). Is there a better
way to do it prefarably without breaking [1]?

TIA,

aa

-- 
Andreas Ames | Programmer | Comergo GmbH |
Voice:  +49 69 7505 3213 | andreas . ames AT comergo . com

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net