티스토리 뷰

참고: http://www.cprogramming.com/c++11/rvalue-references-and-move-semantics-in-c++11.html

#include <iostream>
#include <typeinfo>
#include <vector>
#include <string>
#include <utility>
using namespace std;


class MetaData
{
public:
    MetaData (int size, const std::string& name)
        : _name( name )
        , _size( size )
    {
        cout << "["<< this << "] MetaData default ctor is called."  << endl;
    }

    // copy constructor
    MetaData (const MetaData& other)
        : _name( other._name )
        , _size( other._size )
    {
        cout << "["<< this << "] MetaData copy ctor is called."  << endl;
    }


    // move constructor
    MetaData (MetaData&& other)
        : _name( std::move(other._name) )
        , _size( other._size )
    {
        cout << "["<< this << "] MetaData move ctor is called."  << endl;
    }

    ~MetaData()
    {
        cout << "["<< this << "] metadata dtor is called."  << endl;

    }

    std::string getName () const { return _name; }
    int getSize () const { return _size; }

private:
    std::string _name;
    int _size;
};

template <typename t="">
class ArrayWrapper
{
public:
    // default constructor produces a moderately sized array
    /*
    ArrayWrapper ()
        : _p_vals( new T[ 64 ] )
        , _size( 64 )
    {
        cout << "default ctor is called." << endl;
    }
    */

    ArrayWrapper (int n=64, const string& str="ArrayWrapper")
        : _p_vals( new T[ n ] )
        , _metadata( n, str )
    {
        cout << "<" << _metadata.getName() << ">" << endl;
        cout << "["<< this << "] ArrayWrapper default ctor with size " << n << " is called. " << endl;
    }


    // move constructor
    ArrayWrapper (ArrayWrapper<t>&& other)
        : _p_vals( other._p_vals  )
        , _metadata( std::move(other._metadata) )
    {
        cout << "<" << _metadata.getName() << ">" << endl;
        cout << "["<< this << "] ArrayWrapper move ctor is called."  << endl;
        cout << "other_p_vals: " << _p_vals << endl;
        cout << "my_p_vals: " << _p_vals << endl;

        other._p_vals = NULL;

    }

    // copy constructor
    ArrayWrapper (const ArrayWrapper<t>& other)
        : _p_vals( new T[ other.getSize()  ] )
        , _metadata( other._metadata )
    {
        cout << "<" << _metadata.getName() << ">" << endl;
        cout << "["<< this << "] ArrayWrapper copy ctor is called."  << endl;
        for ( int i = 0; i < _metadata.getSize(); ++i )
        {
            _p_vals[ i ] = other._p_vals[ i ];
        }
    }
    ~ArrayWrapper ()
    {
        cout << "<" << _metadata.getName() << ">" << endl;
        cout << "["<< this << "] ArrayWrapper dtor is called." << endl;
        delete [] _p_vals;
    }

    int getSize() const
    {
        return _metadata.getSize();
    }

private:
    T *_p_vals;
    MetaData _metadata;
};

/*
template <typename t="">
const ArrayWrapper<t> getArrayWarapper()
{
    ArrayWrapper<t> aw(5);
    return aw;
}*/

template <typename t="">
ArrayWrapper<t>&& getArrayWarapper()
{
    return ArrayWrapper<t>(5, "Temp. ArrayWrapper");
}


int main( void ) {

    // getArrayWarapper<int>();
    ArrayWrapper<int> aw = getArrayWarapper<int>();
    ArrayWrapper<int> aw2 = aw;

    cout << "aw: " << aw.getSize() << endl;
    cout << "aw2: " << aw2.getSize() << endl;

    return 0;
}


댓글