some old changes on test_copy_elision.cpp

This commit is contained in:
2014-05-31 21:37:26 +04:00
parent 4a2aec51b3
commit b39968001a

View File

@@ -3,10 +3,10 @@
#include <string>
#include <algorithm>
class copy_tracker
struct copy_tracker
{
int v;
public:
copy_tracker()
: v(0)
{
@@ -16,7 +16,13 @@ public:
copy_tracker( const copy_tracker& c )
: v( c.v )
{
std::cout << "copy_tracker::copy_tracker( copy_tracker& c )" << std::endl;
std::cout << "copy_tracker::copy_tracker( const copy_tracker& c )" << std::endl;
}
copy_tracker( copy_tracker&& c )
: v( c.v )
{
std::cout << "copy_tracker::copy_tracker( copy_tracker&& c ) MOVE " << std::endl;
}
void use_object()
@@ -25,37 +31,123 @@ public:
}
};
copy_tracker copy_tracker_return_by_value2()
template <int n>
copy_tracker return_by_value()
{
return return_by_value<n-1>();
}
template <>
copy_tracker return_by_value<1>()
{
return copy_tracker();
}
copy_tracker copy_tracker_return_by_value()
template <int n>
copy_tracker return_by_value2()
{
return copy_tracker_return_by_value2();
copy_tracker o = return_by_value2<n-1>();
o.v = n;
return o;
}
template <>
copy_tracker return_by_value2<1>()
{
copy_tracker o; //return value optimization still applies.
o.v = 1;
return o;
}
void copy_tracker_pass_by_value2(copy_tracker o)
template <int n>
copy_tracker static_by_value()
{
return o.use_object();
return static_by_value<n-1>();
}
template <>
copy_tracker static_by_value<1>()
{
static copy_tracker o; //return value optimization still applies.
return o;
}
void copy_tracker_pass_by_value(copy_tracker o)
template <int n>
copy_tracker move_by_value()
{
return copy_tracker_pass_by_value2(o);
return std::move(move_by_value<n-1>());
}
template <>
copy_tracker move_by_value<1>()
{
return copy_tracker(); //return value optimization still applies.
}
template <int n>
void pass_by_value(copy_tracker o)
{
pass_by_value<n-1>(std::move(o));
// o.v = n;
// o.use_object();
}
template <>
void pass_by_value<1>(copy_tracker o)
{
o.use_object();
}
void test_copy_tracker()
{
std::cout << "---------------------------------------------------------"
<< std::endl << std::endl;
std::cout << "Return by value and assign." << std::endl;
copy_tracker o = copy_tracker_return_by_value();
{ copy_tracker o = return_by_value<10>(); }
std::cout << "---------------------------------------------------------"
<< std::endl << std::endl;
std::cout << "Return by named value and assign." << std::endl;
{ copy_tracker o = return_by_value2<10>(); }
std::cout << "---------------------------------------------------------"
<< std::endl << std::endl;
std::cout << "Return static and assign." << std::endl;
{ copy_tracker o = static_by_value<10>(); }
std::cout << "---------------------------------------------------------"
<< std::endl << std::endl;
std::cout << "Move value and assign." << std::endl;
{ copy_tracker o = move_by_value<10>(); }
std::cout << "---------------------------------------------------------"
<< std::endl << std::endl;
std::cout << "Pass lvalue." << std::endl;
copy_tracker_pass_by_value( o );
std::cout << "Pass rvalue." << std::endl;
const copy_tracker& o2 = copy_tracker_return_by_value();
copy_tracker_pass_by_value2( o2 );
std::cout << "Pass rvalue 2." << std::endl;
copy_tracker_pass_by_value2( copy_tracker_return_by_value() );
{ copy_tracker o = return_by_value<10>();
pass_by_value<1>( o ); }
std::cout << "---------------------------------------------------------"
<< std::endl << std::endl;
std::cout << "Pass by intermediately captured rvalue." << std::endl;
{ const copy_tracker& o = return_by_value<10>();
pass_by_value<1>( o ); }
std::cout << "---------------------------------------------------------"
<< std::endl << std::endl;
std::cout << "Pass by intermediately captured rvalue." << std::endl;
{ const copy_tracker& o = return_by_value<10>();
pass_by_value<1>( static_cast<copy_tracker&&>(const_cast<copy_tracker&>(o)) ); }
std::cout << "---------------------------------------------------------"
<< std::endl << std::endl;
std::cout << "Pass rvalue directly." << std::endl;
{ pass_by_value<1>( return_by_value<10>() ); }
std::cout << "---------------------------------------------------------"
<< std::endl << std::endl;
std::cout << "Pass rvalue directly." << std::endl;
{ pass_by_value<2>( return_by_value<10>() ); }
std::cout << "---------------------------------------------------------"
<< std::endl << std::endl;
}
int main ( void )