Skip to content

Instantly share code, notes, and snippets.

@kbenzie
Created June 7, 2013 20:22
Show Gist options
  • Save kbenzie/5732138 to your computer and use it in GitHub Desktop.
Save kbenzie/5732138 to your computer and use it in GitHub Desktop.
Example of using std::enable_if
#include <type_traits>
#include <stdio.h>
using namespace std;
template <typename T, typename Ty>
typename enable_if<sizeof(Ty)==sizeof(T), typename T>::type convertToType(Ty type)
{
return *(T*)&type;
}
int main()
{
int i = convertToType<int, float>(46843.0f);
//int i = convertToType<int, double>(78615.0);
printf("%i\n",i);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment