Good question, I thought, I would throw my hat in the ring ... I think you can pass pointers to static variables as arguments of the template type. With C ++ 20, it doesn't seem like it will be a problem ... In the meantime, here are a few cheap macros to make it work.
template <const char *Name, typename T> struct TaggedValue { static constexpr char const *name{Name}; T value; friend ostream &operator<<(ostream &o, const TaggedValue &a) { return o << a.name << " = " << a.value; } }; #define ST(name, type)\ const char ST_name_##name[]{#name};\ using name = TaggedValue<ST_name_##name,type>; ST(Foo, int); ST(Bar, int); ST(Bax, string); int main() { cout << Foo{3} << endl; cout << Bar{5} << endl; cout << Bax{"somthing"} << endl; }
Nathan Chappell May 27 '19 at 11:34 p.m. 2019-05-27 23:34
source share