Created
October 19, 2015 15:49
-
-
Save insertinterestingnamehere/ba7b72997e98626d672d to your computer and use it in GitHub Desktop.
templated implicit conversion
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 <type_traits> | |
template <typename T> | |
struct forward_type { | |
typedef T type; | |
}; | |
class myint { | |
public: | |
int a; | |
myint() = default; | |
template<typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0> | |
myint(const T a) : a(a) {} | |
myint(int a) { this->a = a - 1; } | |
template<typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0> | |
operator T() const { | |
return T(a); | |
} | |
// WHYYYY does this take precedence over the explicitly defined operator int() when that is present...? | |
// WHYYYY does having the template cons and the explicit version non-const make it so everything is routed through the explicit version?? | |
//template<typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0> | |
//operator T() { | |
//return T(a); | |
//} | |
operator int() const { | |
return a + 1; | |
} | |
}; | |
int func(const myint a) { return a.a; } | |
int main() { | |
myint a = 5; | |
int b = a; | |
myint c = b; | |
long long d = c; | |
myint e = d; | |
short f = e; | |
std::cout << func(f); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment