Boost logo

Boost :

From: Robin (darkaurora_at_[hidden])
Date: 2002-05-28 23:10:14


The below snippet shows the cref calls the copy ctor when compiled
with VC++ 6 sp5. I believe it shouldn't occur yet I can't find out
where the copy is created.

I would think making a copy would defeat the purpose of ref. So, can
anyone confirm/suggest alternatives?

Note: Comeau and g++2.95 (mingw) does NOT call the copy ctor.

// BEGIN
#include <boost/ref.hpp>
#include <iostream>

using namespace boost;
using namespace std;

class C
{
public:

       C() : i_(0)
       {
               cout << "Default ctor\n";
       }

       C( int i ) : i_(i)
       {
               cout << "Explicit ctor\n";
       }

       C( const C &c ) : i_(c.i_)
       {
               cout << "Copy ctor\n";
       }

       C& operator=( const C &c )
       {
               cout << "= operator\n";
               i_ = c.i_;
               return *this;
       }

       int i_;
};

void Print( int i, const C &c )
{
       cout << i << " " << c.i_ << endl;
}

int main()
{
       C c(4);

       // Print( 3, c );

       // Generates extra copy ctor in VC++ 6
       Print( 3, cref( c ) );

       return 0;
}


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