|
Boost : |
From: Thorsten Ottosen (nesotto_at_[hidden])
Date: 2002-10-09 15:33:20
Peter Dimov <pdimov_at_[hidden]> wrote:
> Why do you need so many headers?
apparently I don't.
>
> template<class C> class initializer
> {
> public:
>
> explicit initializer(C & c): c_(c)
> {
> }
>
> template<class V> initializer & operator()(V const & v)
> {
> c_.insert(c_.end(), v);
> return *this;
> }
>
> template<class K, class V> initializer & operator()(K const & k, V
const
> & v)
> {
> typedef typename C::value_type P;
> c_.insert(P(k, v));
> return *this;
> }
>
> private:
>
> initializer & operator=(initializer const &);
> C & c_;
> };
>
> template<class C> initializer<C> init(C & c)
> {
> return initializer<C>(c);
> }
>
> Usage:
>
> init(container)(value1)(value2)(value3);
>
> or
>
> init(map)(key1, value1)(key2, value2);
nice stuff! It is considerably simpler than my initial code. I presume you
like the lisp syntax better :-)? As you know, I don't think it is the right
one. One could choose
to support both, though. The problem of missing a value in the assignment to
the map is hypothetical. If I wan't to map int to int, I can make the
mapping just as clear by using newline or tabs:
set_map( m ) += 1,2 2,3 3,4 5,6;
set_map( m ) += 1,2
2,3
3,4;
The first version is the best. One would never miss a value that way and the
values are easier to read than something burried in paranthesis:
init( m )(1,2)(2,3)(3,4);
Now, your templates are really clever. I think that approach would remove
much dependency. However, I think I will still get problems
when I need to use set and stack because insert() is different for set and
stack uses push(). What would be the most elegant way out of this trouple?
regards
-Thorsten
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk