Created
April 4, 2017 00:34
-
-
Save connorworley/2814540faceba51d23de0601f2d6bba7 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
#pragma once | |
#include <type_traits> | |
/* template-arg compatable absolute function */ | |
constexpr inline double abs(double x) { | |
return (x > 0.0) ? x : -x; | |
} | |
/** | |
* C++ doesn't support floating point non-type template arguments so | |
* this is a little hack to let us do static asserts on floats | |
*/ | |
template<int N, int D = 1> | |
struct FakeFloat { | |
static constexpr int numerator = N; | |
static constexpr int denomenator = D; | |
static constexpr double value = static_cast<double>(N) / static_cast<double>(D); | |
}; | |
/** | |
* Here's a struct that we'd like to constrain such that a^2 < abs(b) | |
*/ | |
template<typename A, typename B> | |
struct ConstrainedStruct { | |
static constexpr double a = A::value; | |
static constexpr double b = B::value; | |
ConstrainedStruct(typename std::enable_if<A::value * A::value < abs(B::value)>::type* dummy = 0) { | |
} | |
}; | |
int main(void) { | |
auto x = ConstrainedStruct<FakeFloat<1>, FakeFloat<5, 3>>(); | |
auto y = ConstrainedStruct<FakeFloat<10>, FakeFloat<5, 3>>(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment