enumåãããã®ä¸ã®åã¸ã®ãã£ã¹ã
enumåãããã®ä¸ã®åã¸ã®ãã£ã¹ããã underlying_cast
ãæ¸ããã
#include <cinttypes> #include <type_traits> #include <array> #include <cstdio> template <class E> constexpr typename std::underlying_type<E>::type underlying_cast(E e) noexcept { return static_cast<typename std::underlying_type<E>::type>(e); } enum class Ki : std::uint8_t { momo = 3, kuri = 3, kaki = 8 }; template <class T> void p(T n) { std::puts("something"); } template <> void p(std::uint8_t n) { std::printf("uint8_t %" PRIu8 "\n", n); } int main() { p(underlying_cast(Ki::kaki)); std::array<int, underlying_cast(Ki::kaki)> a{}; return 0; }
$ clang++ -std=c++11 -o underlying_cast underlying_cast.cpp $ ./underlying_cast uint8_t 8
ãã£ããã