Even Modern C++ does not have proper reflection (yet). The Magic Enum library by
Daniil Goncharov fills this void for enum
types, and does
so in a header-only fashion that is eay to deploy (provided
recent-enough compilers).
A compelling example is included in the package, more examples are at the Magic Enum repository.
The following file can be passed to Rcpp::sourceCpp()
as
is:
// [[Rcpp::depends(RcppMagicEnum)]]
#include <RcppMagicEnum>
// define a simple enum class, it uses optional typing as well as optional assigned values
enum class 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
::Rcout << "Name of enum: " << magic_enum::enum_name(val) << std::endl;
Rcpp::Rcout << "Integer value of enum: " << magic_enum::enum_integer(val) << std::endl;
Rcpp}
/*** R
example()
*/
It produces the following output (where the ‘meta-comment’ at the end
ensure the included and created-by-sourcing function
example()
is also called):
> Rcpp::sourceCpp("miniex.cpp")
> example()
: RED
Name of enum: -10
Integer value of enum>
The package is complete and contains a mature version of Magic Enum.
Any problems, bug reports, or features requests for the package can be submitted and handled most conveniently as Github issues in the repository.
Before submitting pull requests, it is frequently preferable to first discuss need and scope in such an issue ticket. See the file Contributing.md (in the Rcpp repo) for a brief discussion.
For the R package, Dirk Eddelbuettel.
For everything pertaining to Magic Enum: Daniil Goncharov.