Skip to content

Instantly share code, notes, and snippets.

@remyroez
Created November 16, 2016 08:58
Show Gist options
  • Save remyroez/06a6b933580d27cfaae42c24e0e2760e to your computer and use it in GitHub Desktop.
Save remyroez/06a6b933580d27cfaae42c24e0e2760e to your computer and use it in GitHub Desktop.
auto_constant
#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