Created
November 18, 2016 02:34
-
-
Save remyroez/d8caafe5fb02c776198083727f15be0e to your computer and use it in GitHub Desktop.
Floating point Constant
This file contains hidden or 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 <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