Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save connorworley/2814540faceba51d23de0601f2d6bba7 to your computer and use it in GitHub Desktop.
Save connorworley/2814540faceba51d23de0601f2d6bba7 to your computer and use it in GitHub Desktop.
#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