Boost logo

Boost :

From: Andy Little (andy_at_[hidden])
Date: 2004-04-05 06:35:10


"Alexander Nasonov" <alnsn-mycop_at_[hidden]> wrote
> Thorsten Ottosen wrote:
>
> >
> > its not global. And you have to include the specific assign/stl.hpp
> > header.

My problem with assignment lib is shown in following.
in a nutshell I might want to use assign , but not +=, from assignment lib.
Currently not possible.
Personally I dont like use of += function for init anyways.
Nevertheless for maps etc and matrices etc it might be useful.
OTOH Adding brackets to each initialiser for assign is a bit ugly... but
appreciate not much you can do about it.

regards
Andy Little

--------------

#include <iostream>
#include <vector>
// stream output for vector of t
#include "test.hpp"
#include "pqs/pqs.hpp"

// if += used for another purpose cant use assign lib
// following cant both be true
#define USE_ARITH_PLUS_EQ 1
#define USE_ASSIGN_LIB 0

#if USE_ASSIGN_LIB
#include "boost/assign.hpp"
using namespace boost::assignment; // bring asignment libs 'operator+=()'
into scope
#endif

#if USE_ARITH_PLUS_EQ
// Dont want assignment lib += semantics,
// rather want to add a value to every element in a vector
template <typename T>
std::vector<T>&
operator +=(std::vector<T>& vect, T t)
{
    std::vector<T>::iterator iter= vect.begin();
    while(iter != vect.end()){
        *iter += t;
        ++iter;
    }
    return vect;
}
#endif

using namespace pqs;
int main()
{
     std::vector<pqs::q_length::mm> v;
// ok cant use += assignment semantics here
// but maybe would like to use assign function
#if USE_ASSIGN_LIB
    assign( v ) (pqs::q_length::mm(1)),
                (pqs::q_length::mm(2)),
                (pqs::q_length::mm(3)),
                (pqs::q_length::mm(4));
#else
    v.push_back(pqs::q_length::mm(1));
    v.push_back(pqs::q_length::mm(2));
    v.push_back(pqs::q_length::mm(3));
    v.push_back(pqs::q_length::mm(4));
#endif

    std::cout << v <<'\n';

#if USE_ARITH_PLUS_EQ // cant do this and use assignment lib
    v += q_length::mm(1);
#endif

    std::cout << v <<'\n';
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk