Created
November 6, 2014 07:40
-
-
Save janekolszak/b9d8fed9aa0833dacc7a to your computer and use it in GitHub Desktop.
Type erasure
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 <memory> | |
#include <functional> | |
using voidFun = void(*)(std::shared_ptr<void>); | |
template<typename T> | |
void fun(std::shared_ptr<T> t) | |
{ | |
std::cout << *t << std::endl; | |
} | |
int main() | |
{ | |
std::function<void(std::shared_ptr<void>)> call; | |
call = reinterpret_cast<voidFun>(fun<std::string>); | |
call(std::make_shared<std::string>("Hi there!")); | |
call = reinterpret_cast<voidFun>(fun<int>); | |
call(std::make_shared<int>(33)); | |
call = reinterpret_cast<voidFun>(fun<char>); | |
call(std::make_shared<int>(33)); | |
// Output:, | |
// Hi there! | |
// 33 | |
// ! | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment