Skip to content

Instantly share code, notes, and snippets.

@Fiona-J-W
Created April 4, 2014 01:08
Show Gist options
  • Select an option

  • Save Fiona-J-W/9966074 to your computer and use it in GitHub Desktop.

Select an option

Save Fiona-J-W/9966074 to your computer and use it in GitHub Desktop.
on_heap_creator
#include <iostream>
#include <utility>
struct foobar{
std::string value = "fooabarbazquux";
foobar() = default;
foobar(const foobar& other): value{other.value} {
std::cout << "foobar(const foobar&)\n";
}
foobar(foobar&& other): value{std::move(other.value)} {
std::cout << "foobar(foobar&&)\n";
}
foobar clone() {
return *this;
}
};
template<typename Function, typename Object, typename...Arguments>
auto on_heap_creator(Function f, Object& o, Arguments&&... args...) -> decltype((o.*f)(args...))* {
using Returntype = decltype((o.*f)(args...));
return new Returntype{std::move((o.*f)(args...))};
}
int main() {
foobar baz;
std::cout << "=========================================\n";
baz.clone();
std::cout << "=========================================\n";
auto x = on_heap_creator(&foobar::clone, baz);
std::cout << "=========================================\n";
delete x;
}
@Fiona-J-W
Copy link
Copy Markdown
Author

Output:

=========================================
foobar(const foobar&)
=========================================
foobar(const foobar&)
foobar(foobar&&)
=========================================

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment