Skip to content

Instantly share code, notes, and snippets.

@kbenzie
Created December 13, 2012 20:24
Show Gist options
  • Save kbenzie/4279480 to your computer and use it in GitHub Desktop.
Save kbenzie/4279480 to your computer and use it in GitHub Desktop.
enable_if example
#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); // Valid
int i = convertToType<int, double>(78615.0); // Invalid
printf("%i\n",i);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment