Created
November 16, 2016 08:58
-
-
Save remyroez/06a6b933580d27cfaae42c24e0e2760e to your computer and use it in GitHub Desktop.
auto_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> | |
// impl ---------- | |
template <auto V> | |
struct auto_constant { | |
static constexpr auto value = V; | |
using value_type = decltype(value); | |
using type = auto_constant; | |
constexpr operator value_type() noexcept { return value; } | |
constexpr value_type operator()() const noexcept { return value; } | |
}; | |
template <auto V> | |
constexpr auto auto_constant_v = auto_constant<V>::value; | |
// usage ---------- | |
constexpr auto foo() { return "hoge"; } | |
int main() | |
{ | |
auto_constant<123> a; | |
std::cout << a() << std::endl; | |
std::cout << auto_constant_v<foo>() << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment