Skip to content

Instantly share code, notes, and snippets.

@kirugan
Last active April 10, 2020 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirugan/63bc4b44660252ebf44b59f1c9d394b4 to your computer and use it in GitHub Desktop.
Save kirugan/63bc4b44660252ebf44b59f1c9d394b4 to your computer and use it in GitHub Desktop.
Playing with template for FixedPoint naive implementation
#include <iostream>
template<std::size_t T>
struct FixedPoint {
static constexpr int Size = T;
int value;
static_assert(Size < sizeof(value) * 8);
};
constexpr FixedPoint test(double d) {
// some logic
}
template<std::size_t T>
void test(FixedPoint<T> point) {
std::cout << point.Size << " " << point.value << " " << sizeof(point) << std::endl;
}
void test(FixedPoint<4> point) {
std::cout << "I'm super optimized version for 4 bits" << point.Size << " " << point.value << " " << sizeof(point) << std::endl;
}
int main() {
{
FixedPoint<5> point;
test(point);
}
{
FixedPoint<4> point;
test(point);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment