Skip to content

Instantly share code, notes, and snippets.

@remyroez
Created November 18, 2016 02:34
Show Gist options
  • Save remyroez/d8caafe5fb02c776198083727f15be0e to your computer and use it in GitHub Desktop.
Save remyroez/d8caafe5fb02c776198083727f15be0e to your computer and use it in GitHub Desktop.
Floating point Constant
#include <iostream>
#include <type_traits>
template <auto *V>
struct floating_point_constant {
static constexpr auto value = *V;
static_assert(std::is_floating_point_v<decltype(value)>);
using value_type = decltype(value);
using type = floating_point_constant;
constexpr operator value_type() noexcept { return value; }
constexpr value_type operator()() const noexcept { return value; }
};
constexpr auto foo = 1.23f;
constexpr auto bar = 4.56;
constexpr auto baz = 789;
int main()
{
std::cout << floating_point_constant<&foo>::value << std::endl;
std::cout << floating_point_constant<&bar>::value << std::endl;
//std::cout << floating_point_constant<&baz>::value << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment