Last active
September 28, 2015 01:55
-
-
Save hansenms/db622fa6022406537e8d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
all: c11_return_opt c11_no_return_opt return_opt no_return_opt | |
c11_return_opt: move_test.cpp | |
g++ -std=c++11 -o c11_return_opt move_test.cpp | |
c11_no_return_opt: move_test.cpp | |
g++ -std=c++11 -fno-elide-constructors -o c11_no_return_opt move_test.cpp | |
return_opt: move_test.cpp | |
g++ -o return_opt move_test.cpp | |
no_return_opt: move_test.cpp | |
g++ -fno-elide-constructors -o no_return_opt move_test.cpp | |
clean: | |
rm c11_return_opt | |
rm c11_no_return_opt | |
rm return_opt | |
rm no_return_opt | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string.h> | |
template <typename T> class Container | |
{ | |
public: | |
Container(size_t s = 0) | |
: d_(0) | |
, s_(s) | |
{ | |
if (s_) { | |
d_ = new T[s_]; | |
} | |
std::cout << "Constructor called in " << this << " (size=" << s_ << ", d=" << d_ << ")" << std::endl; | |
} | |
Container(const Container<T>& o) | |
{ | |
std::cout << "Copy constructor called in " << this << " (size=" << o.s_ << ")" << std::endl; | |
s_ = o.s_; | |
d_ = new T[s_]; | |
memcpy(d_,o.d_,s_*sizeof(T)); | |
} | |
T& operator[](size_t i) | |
{ | |
return d_[i]; | |
} | |
friend void swap(Container<T>& first, Container<T>& second) | |
{ | |
std::swap(first.s_, second.s_); | |
std::swap(first.d_, second.d_); | |
} | |
Container& operator=(Container r) | |
{ | |
std::cout << "Assignment operator called" << std::endl; | |
if (d_) delete [] d_; | |
s_ = r.s_; | |
d_ = new T[s_]; | |
memcpy(d_,r.d_,s_*sizeof(T)); | |
return *this; | |
} | |
#if __cplusplus > 199711L | |
Container(Container<T>&& o) | |
{ | |
std::cout << "MOVE constructor called" << std::endl; | |
//Steal the data | |
s_ = o.s_; | |
d_ = o.d_; | |
//Set object appriately | |
o.d_ = nullptr; | |
o.s_ = 0; | |
} | |
#endif | |
~Container() | |
{ | |
std::cout << "Destructor called, deleting (size=" << s_ << ") " << d_ << " in " << this << std::endl; | |
delete [] d_; | |
} | |
T* data_ptr() | |
{ | |
return d_; | |
} | |
size_t size() | |
{ | |
return s_; | |
} | |
protected: | |
T* d_; | |
size_t s_; | |
}; | |
template <typename T> Container<T> gen_container(size_t s) | |
{ | |
Container<T> c(s); | |
return c; | |
} | |
#if __cplusplus > 199711L | |
template <typename T> std::tuple< Container<T>, Container<T> > gen_containers(size_t s1, size_t s2) | |
{ | |
Container<T> c1(s1); | |
Container<T> c2(s2); | |
return std::make_tuple( std::move(c1), std::move(c2) ); | |
} | |
#endif | |
int main(int argc, char** argv) | |
{ | |
std::cout << "Test of move constructor" << std::endl; | |
{ | |
std::cout << " --- RUNNING SINGLE OUTPUT TEST ---" << std::endl; | |
Container<float> c = gen_container<float>(1000); | |
} | |
#if __cplusplus > 199711L | |
{ | |
std::cout << " --- 1. RUNNING TUPLE OUTPUT TEST ---" << std::endl; | |
Container<float> c1, c2; | |
std::tie(c1, c2) = gen_containers<float>(2000,3000); | |
} | |
{ | |
std::cout << " --- 2. RUNNING TUPLE OUTPUT TEST ---" << std::endl; | |
std::tuple< Container<float>, Container<float> > t = gen_containers<float>(2000,3000); | |
} | |
#endif | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment