Created
November 27, 2018 04:35
-
-
Save pnck/8875c927da7aaa90e6da72a1a1245616 to your computer and use it in GitHub Desktop.
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 <utility> | |
#include <type_traits> | |
template<typename T> | |
struct comparable { | |
virtual const T value() const = 0; | |
}; | |
template<typename T> | |
class A : public comparable<T> { | |
T _n; | |
public: | |
explicit A(const T &v) : _n(v) {} | |
const T value() const override { | |
return _n; | |
} | |
}; | |
struct B : public comparable<float> { | |
float _n; | |
explicit B(const float &v) : _n(v) {} | |
const float value() const override { | |
return _n; | |
} | |
}; | |
#define UNIFORM_VALUE_TYPE double | |
template<typename T, typename U> | |
static bool operator<(const comparable<T> &a, const comparable<U> &b) { | |
return static_cast<UNIFORM_VALUE_TYPE>(a.value()) < static_cast<UNIFORM_VALUE_TYPE>(b.value()); | |
} | |
int main() { | |
std::cout << (A<int>{3} < B{1.2}) << std::endl; | |
std::cout << (A<int>{1} < B{2.2}) << std::endl; | |
std::cout << (B{3.1} < A<int64_t>{1}) << std::endl; | |
std::cout << (B{1.3} < A<int64_t>{22}) << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment