some old changes on test_copy_elision.cpp
This commit is contained in:
@@ -3,10 +3,10 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
class copy_tracker
|
struct copy_tracker
|
||||||
{
|
{
|
||||||
int v;
|
int v;
|
||||||
public:
|
|
||||||
copy_tracker()
|
copy_tracker()
|
||||||
: v(0)
|
: v(0)
|
||||||
{
|
{
|
||||||
@@ -16,7 +16,13 @@ public:
|
|||||||
copy_tracker( const copy_tracker& c )
|
copy_tracker( const copy_tracker& c )
|
||||||
: v( c.v )
|
: 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()
|
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();
|
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()
|
void test_copy_tracker()
|
||||||
{
|
{
|
||||||
|
std::cout << "---------------------------------------------------------"
|
||||||
|
<< std::endl << std::endl;
|
||||||
|
|
||||||
std::cout << "Return by value and assign." << 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;
|
std::cout << "Pass lvalue." << std::endl;
|
||||||
copy_tracker_pass_by_value( o );
|
{ copy_tracker o = return_by_value<10>();
|
||||||
std::cout << "Pass rvalue." << std::endl;
|
pass_by_value<1>( o ); }
|
||||||
const copy_tracker& o2 = copy_tracker_return_by_value();
|
std::cout << "---------------------------------------------------------"
|
||||||
copy_tracker_pass_by_value2( o2 );
|
<< std::endl << std::endl;
|
||||||
std::cout << "Pass rvalue 2." << std::endl;
|
|
||||||
copy_tracker_pass_by_value2( copy_tracker_return_by_value() );
|
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 )
|
int main ( void )
|
||||||
|
|||||||
Reference in New Issue
Block a user