Created
April 4, 2014 01:08
-
-
Save Fiona-J-W/9966074 to your computer and use it in GitHub Desktop.
on_heap_creator
This file contains hidden or 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 <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; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: