Boost logo

Boost Users :

Subject: [Boost-users] [proto] Deep copy question
From: Manjunath Kudlur (keveman_at_[hidden])
Date: 2010-03-05 22:14:55


Hello,

I define the copy constructor for my class and print out out a message
when it is called. See the following program. When I have
"proto::terminal<Var<int> >::type a", and try to call
BOOST_PROTO_AUTO(pr, a), I see that the copy constructor is called two
times. Why is the object getting copied twice during the deep copy?
When I have "proto::literal<Var<int> > a", I see that the copy
constructor is getting called twice during the declaration of 'a'
itself, and twice when I try to call BOOST_PROTO_AUTO(pr, a). I was
under the impression that objects are held by reference inside
expressions. Why are they copied twice in the proto::literal case?
Actually, I am looking for some advice on the following: I know that
when I have proto::terminal<Var<int> >, the underlying object is
created once, and held by reference *everywhere* it is appears in a
large complex expression. But as soon as I try to do a deep_copy of
the expression (as in BOOST_PROTO_AUTO), the underlying object is
copied once for *every* appearance in the expression. How do I make it
such the underlying object doesn't copied many times? I want to have
the copied expression also to have the property that the original
expression had, i.e., everywhere an underlying object is referenced,
they all refer to the same object. I will be grateful for any advice.

#include <boost/proto/proto.hpp>
#include <boost/proto/proto_typeof.hpp>
#include <iostream>
#include <sstream>
#include <string>

namespace proto=boost::proto;
namespace mpl=boost::mpl;

template<typename VT>
struct Var {
  VT value;
  Var(const Var<VT> &that) {
    std::cout << "Copy\n";
    value = that.value;
  }
  Var() {
    std::cout << "Default\n";
    value = 0;
  }
  void assign(const VT v) {
    value = v;
  }
};

typedef proto::literal<Var<int> > int32_;
//typedef proto::terminal<Var<int> >::type int32_;

int main()
{
  int32_ a;

  std::cout << "Before\n";
  BOOST_PROTO_AUTO(pr, (a));
  std::cout << "After\n";
}

Thanks,
Manjunath


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