Skip to content

Instantly share code, notes, and snippets.

@chriswailes
Created July 24, 2012 22:43
Show Gist options
  • Save chriswailes/3173150 to your computer and use it in GitHub Desktop.
Save chriswailes/3173150 to your computer and use it in GitHub Desktop.
Casting Problems with Templates
class MyClass {
template <typename IntType>
inline operator typename std::enable_if<std::is_integral<IntType>::value, IntType>::type () {
// Integer specific code.
}
template <typename FloatType>
inline operator typename std::enable_if<std::is_floating_point<FloatType>::value, FloatType>::type () {
// Float specific code.
}
};
void* foo = malloc(...);
MyClass bar(...);
*reinterpret_cast<uint8_t*>(foo) = static_cast<uint8_t>(bar);
@chriswailes
Copy link
Author

error: no matching conversion for static_cast from 'MyClass' to 'uint8_t' (aka 'unsigned char')
                        *reinterpret_cast<uint8_t*>(foo) = static_cast<uint8_t>(bar));
                                                           ^~~~~~~~~~~~~~~~~~~~~~~~~~
note: candidate template ignored: couldn't infer template argument 'IntType'
        inline operator typename std::enable_if<std::is_integral<IntType>::value, IntType>::type () {
               ^
note: candidate template ignored: couldn't infer template argument 'FloatType'
        inline operator typename std::enable_if<std::is_floating_point<FloatType>::value, FloatType>::type () {
               ^

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