Reflection in C++

Reflection in C++ - Past, Present, and Hopeful Future - Andrei Alexandrescu - CppCon 2022

# Enum

# enchantum (c++17) ⮺

#include <enchantum/enchantum.hpp>

enum class E { V0, V1, V2, V3 };
std::string_view s = enchantum::to_string(E::V0);

# C++26

#include <meta>
template<Enum T>
std::string to_string(T value) { // Could also be marked constexpr
	template for (constexpr auto e : std::meta::members_of(^T)) {
		if ([:e:] == value) {
			return std::string(std::meta::name_of(e));
		}
	}
	return "<unnamed>";
}

# see also

  • Circle lang - a new language that extends C++ 17 to support data-driven imperative metaprogramming. Circle combines the immediacy and flexibility of a scripting language with the type system, performance and universality of C++.
Written on May 11, 2023, Last update on May 14, 2026
c++ reflection enum single-header