|
Boost Users : |
Subject: Re: [Boost-users] [Proto] Questions about deep_copy
From: Kim Kuen Tang (kuentang_at_[hidden])
Date: 2009-05-10 15:52:36
Hi Eric,
thanks for your help.
I will try to integrate this in my code.
B/Rgds
Kim
Eric Niebler schrieb:
> Deep-copying only some terminals is fundamentally unsafe.
> But if you really don't want to deep-copy the vector terminals, you'll
> need to implement your own deep-copy algorithm. It's easiest to write
> a grammar with transforms that does it for you.
> #include <vector>
> #include <boost/proto/proto.hpp>
>
> namespace boost { namespace proto
> {
> // make std::vector a proto terminal extension
> template<typename T, typename Al>
> struct is_extension<std::vector<T, Al> >
> : mpl::true_
> {};
> }}
>
> namespace proto = boost::proto;
> using proto::_;
>
> // Deep-copy all terminals except vectors
> struct DeepCopyNotVectors
> : proto::or_<
> proto::when<
> proto::terminal<std::vector<_,_> >
> // The vector itself will be held by reference, but the
> // temporary proto terminal expression wrapping the
> // reference should be held by value.
> , boost::remove_const<boost::remove_reference<_> >(_)
> >
> , proto::when<proto::terminal<_>, proto::_deep_copy(_)>
> , proto::otherwise<
> proto::nary_expr<_, proto::vararg<DeepCopyNotVectors> >
> >
> >
> {};
>
> int main()
> {
> using namespace proto::exops;
>
> std::vector<int> a, b;
>
> BOOST_AUTO(tmp0, a + b + 0.1);
> BOOST_AUTO(tmp1, DeepCopyNotVectors()(a + b + 0.1));
>
> std::cout << typeid(tmp0).name() << "\n\n";
> std::cout << typeid(tmp1).name() << "\n";
> }
>
> int main()
> {
> using namespace proto::exops;
>
> std::vector<int> a, b;
>
> BOOST_AUTO(tmp0, a + b + 0.1);
> BOOST_AUTO(tmp1, DeepCopyNotVectors()(a + b + 0.1));
>
> std::cout << typeid(tmp0).name() << "\n\n";
> std::cout << typeid(tmp1).name() << "\n";
> }
>
>
>
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