Even Modern C++ does not have proper reflection (yet). TheMagic Enum library byDaniil Goncharov fills this void forenum types, and doesso in a header-only fashion that is eay to deploy (providedrecent-enough compilers).
A compelling example is included in the package, more examples are attheMagic Enumrepository.
The following file can be passed toRcpp::sourceCpp() asis:
// [[Rcpp::depends(RcppMagicEnum)]]#include<RcppMagicEnum>// define a simple enum class, it uses optional typing as well as optional assigned valuesenumclass Color:int{ RED=-10, BLUE=0, GREEN=10};// [[Rcpp::export]]void example(){// instantiate an enum value in variable 'val'auto val= Color::RED;// show the current value on stdout Rcpp::Rcout<<"Name of enum: "<< magic_enum::enum_name(val)<<std::endl; Rcpp::Rcout<<"Integer value of enum: "<< magic_enum::enum_integer(val)<<std::endl;}/*** Rexample()*/It produces the following output (where the ‘meta-comment’ at the endensure the included and created-by-sourcing functionexample() is also called):
> Rcpp::sourceCpp("miniex.cpp")>example()Name of enum: REDInteger value of enum:-10>The package is complete and contains a mature version of MagicEnum.
Any problems, bug reports, or features requests for the package canbe submitted and handled most conveniently asGithubissues in the repository.
Before submitting pull requests, it is frequently preferable to firstdiscuss need and scope in such an issue ticket. See the fileContributing.md(in theRcpp repo) for abrief discussion.
For the R package,DirkEddelbuettel.
For everything pertaining to Magic Enum:Daniil Goncharov.
Initially created: Tue Aug 20 04:46:22 PM CDT 2024
Last modified: Tue Aug 20 16:48:31 CDT 2024