<p>I'd like to be able to introspect a C++ class for its name, contents (i.e. members and their types) etc. I'm talking native C++ here, not managed C++, which has reflection. I realise C++ supplies some limited information using RTTI. Which additional libraries (or other techniques) could supply this information?</p>

[{'answer_id': 41459, 'author': 'Brad Wilson', 'author_id': 1554, 'author_profile': 'https://Stackoverflow.com/users/1554', 'pm_score': 6, 'selected': False, 'text': '<p>And I would love a pony, but ponies aren\'t free. :-p</p>\n\n<p><a href="http://en.wikibooks.org/wiki/C%2B%2B_Programming/RTTI" rel="noreferrer">http://en.wikibooks.org/wiki/C%2B%2B_Programming/RTTI</a> is what you\'re going to get. Reflection like you\'re thinking about -- fully descriptive metadata available at runtime -- just doesn\'t exist for C++ by default.</p>\n'}, {'answer_id': 41483, 'author': 'Ferruccio', 'author_id': 4086, 'author_profile': 'https://Stackoverflow.com/users/4086', 'pm_score': 4, 'selected': False, 'text': '<p>What are you trying to do with reflection?\n<br>\nYou can use the Boost <a href="http://www.boost.org/doc/libs/1_36_0/libs/type_traits/doc/html/index.html" rel="noreferrer">type traits</a> and <a href="http://www.boost.org/doc/libs/1_36_0/doc/html/typeof.html" rel="noreferrer">typeof</a> libraries as a limited form of compile-time reflection. That is, you can inspect and modify the basic properties of a type passed to a template.</p>\n'}, {'answer_id': 41528, 'author': 'user4385', 'author_id': 4385, 'author_profile': 'https://Stackoverflow.com/users/4385', 'pm_score': 3, 'selected': False, 'text': '<p>The two reflection-like solutions I know of from my C++ days are:</p>\n\n<p>1) Use RTTI, which will provide a bootstrap for you to build your reflection-like behaviour, if you are able to get all your classes to derive from an \'object\' base class. That class could provide some methods like GetMethod, GetBaseClass etc. As for how those methods work you will need to manually add some macros to decorate your types, which behind the scenes create metadata in the type to provide answers to GetMethods etc.</p>\n\n<p>2) Another option, if you have access to the compiler objects is to use the <a href="http://msdn.microsoft.com/en-us/library/t6tay6cz(VS.80).aspx" rel="noreferrer">DIA SDK</a>. If I remember correctly this lets you open pdbs, which should contain metadata for your C++ types. It might be enough to do what you need. <a href="http://www.vsj.co.uk/dotnet/display.asp?id=320" rel="noreferrer">This page</a> shows how you can get all base types of a class for example.</p>\n\n<p>Both these solution are a bit ugly though! There is nothing like a bit of C++ to make you appreciate the luxuries of C#.</p>\n\n<p>Good Luck.</p>\n'}, {'answer_id': 41618, 'author': 'Jérôme', 'author_id': 2796, 'author_profile': 'https://Stackoverflow.com/users/2796', 'pm_score': 4, 'selected': False, 'text': '<p>I would recommend using <a href="https://www.qt.io/" rel="nofollow noreferrer">Qt</a>.</p>\n\n<p>There is an open-source licence as well as a commercial licence.</p>\n'}, {'answer_id': 41865, 'author': 'KeithB', 'author_id': 2298, 'author_profile': 'https://Stackoverflow.com/users/2298', 'pm_score': 4, 'selected': False, 'text': '<p>You need to look at what you are trying to do, and if RTTI will satisfy your requirements.  I\'ve implemented my own pseudo-reflection for some very specific purposes.  For example, I once wanted to be able to flexibly configure what a simulation would output.  It required adding some boilerplate code to the classes that would be output:</p>\n\n<pre><code>namespace {\n  static bool b2 = Filter::Filterable&lt;const MyObj&gt;::Register("MyObject");\n} \n\nbool MyObj::BuildMap()\n{\n  Filterable&lt;const OutputDisease&gt;::AddAccess("time", &amp;MyObj::time);\n  Filterable&lt;const OutputDisease&gt;::AddAccess("person", &amp;MyObj::id);\n  return true;\n}\n</code></pre>\n\n<p>The first call adds this object to the filtering system, which calls the <code>BuildMap()</code> method to figure out what methods are available.  </p>\n\n<p>Then, in the config file, you can do something like this:</p>\n\n<pre><code>FILTER-OUTPUT-OBJECT   MyObject\nFILTER-OUTPUT-FILENAME file.txt\nFILTER-CLAUSE-1        person == 1773\nFILTER-CLAUSE-2        time &gt; 2000\n</code></pre>\n\n<p>Through some template magic involving <code>boost</code>, this gets translated into a series of method calls at run-time (when the config file is read), so it\'s fairly efficient.  I wouldn\'t recommend doing this unless you really need to, but, when you do, you can do some really cool stuff.</p>\n'}, {'answer_id': 314252, 'author': 'Luis', 'author_id': 22609, 'author_profile': 'https://Stackoverflow.com/users/22609', 'pm_score': 3, 'selected': False, 'text': '<p>I think you might find interesting the article "Using Templates for Reflection in C++" by Dominic Filion. It is in section 1.4 of <a href="https://books.google.de/books/about/Game_Programming_Gems_5.html?id=9iN0QgAACAAJ&amp;redir_esc=y" rel="nofollow noreferrer" title="Game Programming Gems 5">Game Programming Gems 5</a>. Unfortunately I dont have my copy with me, but look for it because I think it explains what you are asking for.</p>\n'}, {'answer_id': 314282, 'author': 'Johannes Schaub - litb', 'author_id': 34509, 'author_profile': 'https://Stackoverflow.com/users/34509', 'pm_score': 7, 'selected': False, 'text': '<p>There are two kinds of <code>reflection</code> swimming around.</p>\n<ol>\n<li>Inspection by iterating over members of a type, enumerating its methods and so on.<br /><br />\nThis is not possible with C++. </li>\n<li>Inspection by checking whether a class-type (class, struct, union) has a method or nested type, is derived from another particular type.<br /><br />\nThis kind of thing is possible with C++ using <code>template-tricks</code>. Use <a href="http://www.boost.org/doc/libs/master/libs/type_traits/doc/html/index.html" rel="nofollow noreferrer"><code>boost::type_traits</code></a> for many things (like checking whether a type is integral). For checking for the existence of a member function, use <a href="https://stackoverflow.com/questions/257288/possible-for-c-template-to-check-for-a-functions-existence#264088">Templated check for the existence of a class member function?</a> . For checking whether a certain nested type exists, use plain <a href="http://en.wikipedia.org/wiki/SFINAE" rel="nofollow noreferrer">SFINAE</a> .</li>\n</ol>\n<p>If you are rather looking for ways to accomplish 1), like looking how many methods a class has, or like getting the string representation of a class id, then I\'m afraid there is no Standard C++ way of doing this. You have to use either</p>\n<ul>\n<li>A Meta Compiler like the Qt Meta Object Compiler which translates your code adding additional meta information.</li>\n<li>A Framework consisting of macros that allow you to add the required meta-information. You would need to tell the framework all methods, the class-names, base-classes and everything it needs.</li>\n</ul>\n<p>C++ is made with speed in mind. If you want high-level inspection, like C# or Java has, there is no way to do that without some additional effort.</p>\n'}, {'answer_id': 324689, 'author': 'Michel', 'author_id': 31122, 'author_profile': 'https://Stackoverflow.com/users/31122', 'pm_score': 4, 'selected': False, 'text': '<p>I did something like what you\'re after once, and while it\'s possible to get some level of reflection and access to higher-level features, the maintenance headache might not be worth it.  My system was used to keep the UI classes completely separated from the business logic through delegation akin to Objective-C\'s concept of message passing and forwarding.  The way to do it is to create some base class that is capable of mapping symbols (I used a string pool but you could do it with enums if you prefer speed and compile-time error handling over total flexibility) to function pointers (actually not pure function pointers, but something similar to what Boost has with Boost.Function--which I didn\'t have access to at the time).  You can do the same thing for your member variables as long as you have some common base class capable of representing any value.  The entire system was an unabashed ripoff of Key-Value Coding and Delegation, with a few side effects that were perhaps worth the sheer amount of time necessary to get every class that used the system to match all of its methods and members up with legal calls:  1)  Any class could call any method on any other class without having to include headers or write fake base classes so the interface could be predefined for the compiler; and 2)  The getters and setters of the member variables were easy to make thread-safe because changing or accessing their values was always done through 2 methods in the base class of all objects.</p>\n\n<p>It also led to the possibility of doing some really weird things that otherwise aren\'t easy in C++.  For example I could create an Array object that contained arbitrary items of any type, including itself, and create new arrays dynamically by passing a message to all array items and collecting the return values (similar to map in Lisp).  Another was the implementation of key-value observing, whereby I was able to set up the UI to respond immediately to changes in the members of backend classes instead of constantly polling the data or unnecessarily redrawing the display.</p>\n\n<p>Maybe more interesting to you is the fact that you can also dump all methods and members defined for a class, and in string form no less.</p>\n\n<p>Downsides to the system that might discourage you from bothering:  adding all of the messages and key-values is extremely tedious; it\'s slower than without any reflection; you\'ll grow to hate seeing <code>boost::static_pointer_cast</code> and <code>boost::dynamic_pointer_cast</code> all over your codebase with a violent passion; the limitations of the strongly-typed system are still there, you\'re really just hiding them a bit so it isn\'t as obvious.  Typos in your strings are also not a fun or easy to discover surprise.</p>\n\n<p>As to how to implement something like this:  just use shared and weak pointers to some common base (mine was very imaginatively called "Object") and derive for all the types you want to use.  I\'d recommend installing Boost.Function instead of doing it the way I did, which was with some custom crap and a ton of ugly macros to wrap the function pointer calls.  Since everything is mapped, inspecting objects is just a matter of iterating through all of the keys.  Since my classes were essentially as close to a direct ripoff of Cocoa as possible using only C++, if you want something like that then I\'d suggest using the Cocoa documentation as a blueprint.</p>\n'}, {'answer_id': 1300289, 'author': 'Roderick', 'author_id': 134919, 'author_profile': 'https://Stackoverflow.com/users/134919', 'pm_score': 5, 'selected': False, 'text': '<p>The information does exist - but not in the format you need, and only if you export your classes. This works in Windows, I don\'t know about other platforms. Using the storage-class specifiers as in, for example:</p>\n\n<pre><code>class __declspec(export) MyClass\n{\npublic:\n    void Foo(float x);\n}\n</code></pre>\n\n<p>This makes the compiler build the class definition data into the DLL/Exe. But it\'s not in a format that you can readily use for reflection.</p>\n\n<p>At my company we built a library that interprets this metadata, and allows you to reflect a class without inserting extra macros etc. into the class itself. It allows functions to be called as follows:</p>\n\n<pre><code>MyClass *instance_ptr=new MyClass;\nGetClass("MyClass")-&gt;GetFunction("Foo")-&gt;Invoke(instance_ptr,1.331);\n</code></pre>\n\n<p>This effectively does:</p>\n\n<pre><code>instance_ptr-&gt;Foo(1.331);\n</code></pre>\n\n<p>The Invoke(this_pointer,...) function has variable arguments. Obviously by calling a function in this way you\'re circumventing things like const-safety and so on, so these aspects are implemented as runtime checks.</p>\n\n<p>I\'m sure the syntax could be improved, and it only works on Win32 and Win64 so far. We\'ve found it really useful for having automatic GUI interfaces to classes, creating properties in C++, streaming to and from XML and so on, and there\'s no need to derive from a specific base class. If there\'s enough demand maybe we could knock it into shape for release.</p>\n'}, {'answer_id': 3034896, 'author': 'Edward Strange', 'author_id': 301883, 'author_profile': 'https://Stackoverflow.com/users/301883', 'pm_score': 2, 'selected': False, 'text': '<p>When I wanted reflection in C++ I read <a href="http://lcgapp.cern.ch/project/architecture/ReflectionPaper.pdf" rel="nofollow noreferrer">this article</a> and improved upon what I saw there.  Sorry, no can has.  I don\'t own the result...but you can certainly get what I had and go from there.</p>\n\n<p>I am currently researching, when I feel like it, methods to use inherit_linearly to make the definition of reflectable types much easier.  I\'ve gotten fairly far in it actually but I still have a ways to go.  The changes in C++0x are very likely to be a lot of help in this area.</p>\n'}, {'answer_id': 3080385, 'author': 'philant', 'author_id': 18804, 'author_profile': 'https://Stackoverflow.com/users/18804', 'pm_score': 4, 'selected': False, 'text': '<p><strong>EDIT</strong>: <a href="https://github.com/tegesoft/camp" rel="nofollow noreferrer">CAMP</a> is no more maintained ; two forks are available: </p>\n\n<ul>\n<li>One is also called <a href="https://github.com/fw4spl-org/camp" rel="nofollow noreferrer">CAMP</a> too, and is based on the same API.</li>\n<li><a href="https://github.com/billyquith/ponder" rel="nofollow noreferrer">Ponder</a> is a partial rewrite, and shall be preferred as it does not requires Boost ; it\'s using C++11.</li>\n</ul>\n\n<hr>\n\n<p><a href="https://github.com/tegesoft/camp" rel="nofollow noreferrer">CAMP</a> is an MIT licensed library (formerly LGPL) that adds reflection to the C++ language. It doesn\'t require a specific preprocessing step in the compilation, but the binding has to be made manually.</p>\n\n<p>The current Tegesoft library uses Boost, but there is also <a href="https://github.com/billyquith/camp" rel="nofollow noreferrer">a fork</a> using C++11 that <em>no longer requires Boost</em>.</p>\n'}, {'answer_id': 4167176, 'author': 'Matthieu M.', 'author_id': 147192, 'author_profile': 'https://Stackoverflow.com/users/147192', 'pm_score': 3, 'selected': False, 'text': '<p>This question is a bit old now (don\'t know why I keep hitting old questions today) but I was thinking about <a href="http://boost-spirit.com/dl_more/fusion_v2/libs/fusion/doc/html/fusion/extension/macros/adapt_struct.html" rel="noreferrer">BOOST_FUSION_ADAPT_STRUCT</a> which introduces compile-time reflection.</p>\n\n<p>It is up to you to map this to run-time reflection of course, and it won\'t be too easy, but it is possible in this direction, while it would not be in the reverse :)</p>\n\n<p>I really think a macro to encapsulate the <code>BOOST_FUSION_ADAPT_STRUCT</code> one could generate the necessary methods to get the runtime behavior.</p>\n'}, {'answer_id': 4562038, 'author': 'Ira Baxter', 'author_id': 120163, 'author_profile': 'https://Stackoverflow.com/users/120163', 'pm_score': 3, 'selected': False, 'text': '<p>Reflection is essentially about what the compiler decided to leave as footprints in the code that the runtime code can query.  C++ is famous for not paying for what you don\'t use; because most people don\'t use/want reflection, the C++ compiler avoids the cost by not recording <em>anything</em>.</p>\n\n<p>So, C++ doesn\'t provide reflection, and it isn\'t easy to "simulate" it yourself as general rule as other answers have noted.</p>\n\n<p>Under "other techniques", if you don\'t have a language with reflection, <strong><em>get a tool that can extract the information you want at compile time.</em></strong></p>\n\n<p>Our <a href="http://www.semanticdesigns.com/Products/DMS/DMSToolkit.html" rel="nofollow noreferrer">DMS Software Reengineering Toolkit</a> is generalized compiler technology parameterized by explicit langauge definitions. It has langauge definitions for C, C++, Java, COBOL, PHP, ...</p>\n\n<p>For C, C++, Java and COBOL versions, it provides complete access to parse trees, and symbol table information.  That symbol table information includes the kind of data you are likely to want from "reflection".  If you goal is to enumerate some set of fields or methods and <em>do</em> something with them, DMS can be used to transform the code according to what you find in the symbol tables in arbitrary ways.</p>\n'}, {'answer_id': 6856270, 'author': 'alariq', 'author_id': 837614, 'author_profile': 'https://Stackoverflow.com/users/837614', 'pm_score': 2, 'selected': False, 'text': '<p>You can find another library here: <a href="http://www.garret.ru/cppreflection/docs/reflect.html" rel="nofollow">http://www.garret.ru/cppreflection/docs/reflect.html</a>\nIt supports 2 ways: getting type information from debug information and let programmer to provide this information.</p>\n\n<p>I also interested in reflection for my project and found this library, i have not tried it yet, but tried other tools from this guy and i like how they work :-)</p>\n'}, {'answer_id': 8253994, 'author': 'Bohdan', 'author_id': 874275, 'author_profile': 'https://Stackoverflow.com/users/874275', 'pm_score': 2, 'selected': False, 'text': '<p>It looks like C++ still does not have this feature.\nAnd <strong><a href="http://en.wikipedia.org/wiki/C++11#Features_planned_but_removed_or_not_included" rel="nofollow">C++11</a></strong> postponed reflection too ((</p>\n\n<p>Search some macros or make own. Qt also can help with reflection (if it can be used).</p>\n'}, {'answer_id': 11748131, 'author': 'Paul Fultz II', 'author_id': 375343, 'author_profile': 'https://Stackoverflow.com/users/375343', 'pm_score': 8, 'selected': False, 'text': '<p>What you need to do is have the preprocessor generate reflection data about the fields. This data can be stored as nested classes. </p>\n\n<p>First, to make it easier and cleaner to write it in the preprocessor we will use typed expression. A typed expression is just an expression that puts the type in parenthesis. So instead of writing <code>int x</code> you will write <code>(int) x</code>. Here are some handy macros to help with typed expressions:</p>\n\n<pre><code>#define REM(...) __VA_ARGS__\n#define EAT(...)\n\n// Retrieve the type\n#define TYPEOF(x) DETAIL_TYPEOF(DETAIL_TYPEOF_PROBE x,)\n#define DETAIL_TYPEOF(...) DETAIL_TYPEOF_HEAD(__VA_ARGS__)\n#define DETAIL_TYPEOF_HEAD(x, ...) REM x\n#define DETAIL_TYPEOF_PROBE(...) (__VA_ARGS__),\n// Strip off the type\n#define STRIP(x) EAT x\n// Show the type without parenthesis\n#define PAIR(x) REM x\n</code></pre>\n\n<p>Next, we define a <code>REFLECTABLE</code> macro to generate the data about each field(plus the field itself). This macro will be called like this:</p>\n\n<pre><code>REFLECTABLE\n(\n    (const char *) name,\n    (int) age\n)\n</code></pre>\n\n<p>So using <a href="http://www.boost.org/doc/libs/1_49_0/libs/preprocessor/doc/index.html" rel="noreferrer">Boost.PP</a> we iterate over each argument and generate the data like this:</p>\n\n<pre><code>// A helper metafunction for adding const to a type\ntemplate&lt;class M, class T&gt;\nstruct make_const\n{\n    typedef T type;\n};\n\ntemplate&lt;class M, class T&gt;\nstruct make_const&lt;const M, T&gt;\n{\n    typedef typename boost::add_const&lt;T&gt;::type type;\n};\n\n\n#define REFLECTABLE(...) \\\nstatic const int fields_n = BOOST_PP_VARIADIC_SIZE(__VA_ARGS__); \\\nfriend struct reflector; \\\ntemplate&lt;int N, class Self&gt; \\\nstruct field_data {}; \\\nBOOST_PP_SEQ_FOR_EACH_I(REFLECT_EACH, data, BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__))\n\n#define REFLECT_EACH(r, data, i, x) \\\nPAIR(x); \\\ntemplate&lt;class Self&gt; \\\nstruct field_data&lt;i, Self&gt; \\\n{ \\\n    Self &amp; self; \\\n    field_data(Self &amp; self) : self(self) {} \\\n    \\\n    typename make_const&lt;Self, TYPEOF(x)&gt;::type &amp; get() \\\n    { \\\n        return self.STRIP(x); \\\n    }\\\n    typename boost::add_const&lt;TYPEOF(x)&gt;::type &amp; get() const \\\n    { \\\n        return self.STRIP(x); \\\n    }\\\n    const char * name() const \\\n    {\\\n        return BOOST_PP_STRINGIZE(STRIP(x)); \\\n    } \\\n}; \\\n</code></pre>\n\n<p>What this does is generate a constant <code>fields_n</code> that is number of reflectable fields in the class. Then it specializes the <code>field_data</code> for each field. It also friends the <code>reflector</code> class, this is so it can access the fields even when they are private:</p>\n\n<pre><code>struct reflector\n{\n    //Get field_data at index N\n    template&lt;int N, class T&gt;\n    static typename T::template field_data&lt;N, T&gt; get_field_data(T&amp; x)\n    {\n        return typename T::template field_data&lt;N, T&gt;(x);\n    }\n\n    // Get the number of fields\n    template&lt;class T&gt;\n    struct fields\n    {\n        static const int n = T::fields_n;\n    };\n};\n</code></pre>\n\n<p>Now to iterate over the fields we use the visitor pattern. We create an MPL range from 0 to the number of fields, and access the field data at that index. Then it passes the field data on to the user-provided visitor:</p>\n\n<pre><code>struct field_visitor\n{\n    template&lt;class C, class Visitor, class I&gt;\n    void operator()(C&amp; c, Visitor v, I)\n    {\n        v(reflector::get_field_data&lt;I::value&gt;(c));\n    }\n};\n\n\ntemplate&lt;class C, class Visitor&gt;\nvoid visit_each(C &amp; c, Visitor v)\n{\n    typedef boost::mpl::range_c&lt;int,0,reflector::fields&lt;C&gt;::n&gt; range;\n    boost::mpl::for_each&lt;range&gt;(boost::bind&lt;void&gt;(field_visitor(), boost::ref(c), v, _1));\n}\n</code></pre>\n\n<p>Now for the moment of truth we put it all together. Here is how we can define a <code>Person</code> class that is reflectable:</p>\n\n<pre><code>struct Person\n{\n    Person(const char *name, int age)\n        :\n        name(name),\n        age(age)\n    {\n    }\nprivate:\n    REFLECTABLE\n    (\n        (const char *) name,\n        (int) age\n    )\n};\n</code></pre>\n\n<p>Here is a generalized <code>print_fields</code> function using the reflection data to iterate over the fields:</p>\n\n<pre><code>struct print_visitor\n{\n    template&lt;class FieldData&gt;\n    void operator()(FieldData f)\n    {\n        std::cout &lt;&lt; f.name() &lt;&lt; "=" &lt;&lt; f.get() &lt;&lt; std::endl;\n    }\n};\n\ntemplate&lt;class T&gt;\nvoid print_fields(T &amp; x)\n{\n    visit_each(x, print_visitor());\n}\n</code></pre>\n\n<p>An example of using the <code>print_fields</code> with the reflectable <code>Person</code> class:</p>\n\n<pre><code>int main()\n{\n    Person p("Tom", 82);\n    print_fields(p);\n    return 0;\n}\n</code></pre>\n\n<p>Which outputs:</p>\n\n<pre><code>name=Tom\nage=82\n</code></pre>\n\n<p>And voila, we have just implemented reflection in C++, in under 100 lines of code. </p>\n'}, {'answer_id': 13217106, 'author': 'Damian Dixon', 'author_id': 639324, 'author_profile': 'https://Stackoverflow.com/users/639324', 'pm_score': 6, 'selected': False, 'text': '<p>Reflection is not supported by C++ out of the box. This is sad because it makes defensive testing a pain.</p>\n<p>There are several approaches to doing reflection:</p>\n<ol>\n<li>use the debug information (non portable).</li>\n<li>Sprinkle your code with macro\'s/templates or some other source approach (looks ugly)</li>\n<li>Modify a compiler such as clang/gcc to produce a database.</li>\n<li>Use Qt moc approach</li>\n<li><a href="http://bytemaster.github.io/boost_reflect/" rel="noreferrer">Boost Reflect</a></li>\n<li><a href="https://github.com/apolukhin/magic_get" rel="noreferrer">Precise and Flat Reflection</a></li>\n</ol>\n<p>The first link looks the most promising (uses mod\'s to clang), the second discusses a number of techniques, the third is a different approach using gcc:</p>\n<ol>\n<li><p><a href="http://www.donw.org/rfl/" rel="noreferrer">http://www.donw.org/rfl/</a></p>\n</li>\n<li><p><a href="https://bitbucket.org/dwilliamson/clreflect" rel="noreferrer">https://bitbucket.org/dwilliamson/clreflect</a></p>\n</li>\n<li><p><a href="https://root.cern.ch/how/how-use-reflex" rel="noreferrer">https://root.cern.ch/how/how-use-reflex</a></p>\n</li>\n</ol>\n<p>There is now a working group for C++ reflection. See the news for C++14 @ CERN:</p>\n<ul>\n<li><a href="https://root.cern.ch/blog/status-reflection-c" rel="noreferrer">https://root.cern.ch/blog/status-reflection-c</a></li>\n</ul>\n<p><strong>Edit 13/08/17:</strong></p>\n<p>Since the  original post there have been a number of potential advancements on the reflection. The following provides more detail and a discussion on the various techniques and status:</p>\n<ol>\n<li><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0578r0.html" rel="noreferrer">Static Reflection in a Nutshell</a></li>\n<li><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0194r3.html" rel="noreferrer">Static Reflection</a></li>\n<li><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0590r0.pdf" rel="noreferrer">A design for static reflection</a></li>\n</ol>\n<p>However it does not look promising on a standardised reflections approach in C++ in the near future unless there is a lot more interest from the community in support for reflection in C++.</p>\n<p>The following details the current status based on feedback from the last C++ standards meeting:</p>\n<ul>\n<li><a href="https://meetingcpp.com/index.php/br/items/reflections-on-the-reflection-proposals.html" rel="noreferrer">Reflections on the reflection proposals</a></li>\n</ul>\n<p><strong>Edit 13/12/2017</strong></p>\n<p>Reflection looks to be moving towards C++ 20 or more probably a TSR. Movement is however slow.</p>\n<ul>\n<li><a href="http://matus-chochlik.github.io/mirror/doc/html/" rel="noreferrer">Mirror</a></li>\n<li><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0194r2.html" rel="noreferrer">Mirror standard proposal</a></li>\n<li><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0385r1.pdf" rel="noreferrer">Mirror paper</a></li>\n<li><a href="https://herbsutter.com/2017/07/26/metaclasses-thoughts-on-generative-c/" rel="noreferrer">Herb Sutter - meta programming including reflection</a></li>\n</ul>\n<p><strong>Edit 15/09/2018</strong></p>\n<p>A draft TS has been sent out to the national bodies for ballot.</p>\n<p>The text can be found here: <a href="https://github.com/cplusplus/reflection-ts" rel="noreferrer">https://github.com/cplusplus/reflection-ts</a></p>\n<p><strong>Edit 11/07/2019</strong></p>\n<p>The reflection TS is feature complete and is out for comment and vote over the summer (2019).</p>\n<p>The meta-template programing approach is to be replaced with a simplier compile time code approach (not reflected in the TS).</p>\n<ul>\n<li><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/n4818.pdf" rel="noreferrer">Draft TS as of 2019-06-17</a></li>\n</ul>\n<p><strong>Edit 10/02/2020</strong></p>\n<p>There is a request to support the reflection TS in Visual Studio here:</p>\n<ul>\n<li><a href="https://developercommunity.visualstudio.com/idea/826632/implement-the-c-reflection-ts.html" rel="noreferrer">https://developercommunity.visualstudio.com/idea/826632/implement-the-c-reflection-ts.html</a></li>\n</ul>\n<p>Talk on the TS by the author David Sankel:</p>\n<ul>\n<li><p><a href="http://cppnow.org/history/2019/talks/" rel="noreferrer">http://cppnow.org/history/2019/talks/</a></p>\n</li>\n<li><p><a href="https://www.youtube.com/watch?v=VMuML6vLSus&amp;feature=youtu.be" rel="noreferrer">https://www.youtube.com/watch?v=VMuML6vLSus&amp;feature=youtu.be</a></p>\n</li>\n</ul>\n<p><strong>Edit 17 March 2020</strong></p>\n<p>Progress on reflection is being made. A report from \'2020-02 Prague ISO C++ Committee Trip Report\' can be found here:</p>\n<ul>\n<li><a href="https://www.reddit.com/r/cpp/comments/f47x4o/202002_prague_iso_c_committee_trip_report_c20_is/" rel="noreferrer">https://www.reddit.com/r/cpp/comments/f47x4o/202002_prague_iso_c_committee_trip_report_c20_is/</a></li>\n</ul>\n<p>Details on what is being considered for C++23 can be found here (includes short section on Reflection):</p>\n<ul>\n<li><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0592r4.html" rel="noreferrer">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0592r4.html</a></li>\n</ul>\n<p><strong>Edit 4th June 2020</strong></p>\n<p>A new framework has been released by Jeff Preshing called \'Plywood\' that contains a mechanism for runtime reflection. More details can be found here:</p>\n<ul>\n<li><a href="https://preshing.com/20200526/a-new-cross-platform-open-source-cpp-framework/" rel="noreferrer">https://preshing.com/20200526/a-new-cross-platform-open-source-cpp-framework/</a></li>\n</ul>\n<p>The tools and approach look to be the most polished and easiest to use so far.</p>\n<p><strong>Edit July 12 2020</strong></p>\n<p>Clang experimental reflection fork : <a href="https://github.com/lock3/meta/wiki" rel="noreferrer">https://github.com/lock3/meta/wiki</a></p>\n<p>Interesting reflection library that uses clang tooling library to extract information for simple reflection with no need to add macro\'s: <a href="https://github.com/chakaz/reflang" rel="noreferrer">https://github.com/chakaz/reflang</a></p>\n<p><strong>Edit Feb 24 2021</strong></p>\n<p>Some additional clang tooling approaches:</p>\n<ul>\n<li><a href="https://github.com/Celtoys/clReflect" rel="noreferrer">https://github.com/Celtoys/clReflect</a></li>\n<li><a href="https://github.com/mlomb/MetaCPP" rel="noreferrer">https://github.com/mlomb/MetaCPP</a></li>\n</ul>\n<p><strong>Edit Aug 25 2021</strong></p>\n<p>An ACCU talk online at youtube <a href="https://www.youtube.com/watch?v=60ECEc-URP8" rel="noreferrer">https://www.youtube.com/watch?v=60ECEc-URP8</a> is well worth a listen too it talks about current proposals to the standard and an implementation based on clang.</p>\n<p>See:</p>\n<ul>\n<li><a href="https://github.com/lock3/meta" rel="noreferrer">https://github.com/lock3/meta</a>, branch paper/p2320</li>\n<li>Compiler Explorer : <a href="https://cppx.godbolt.org/" rel="noreferrer">https://cppx.godbolt.org/</a> use the p2320 trunk for the compiler version.</li>\n</ul>\n'}, {'answer_id': 14667603, 'author': 'Naore Azenkut', 'author_id': 1941392, 'author_profile': 'https://Stackoverflow.com/users/1941392', 'pm_score': 2, 'selected': False, 'text': '<p>even though reflection is not supported out-of-the-box in c++, it is not too hard to implement.\nI\'ve encountered this great article:\n<a href="http://replicaisland.blogspot.co.il/2010/11/building-reflective-object-system-in-c.html" rel="nofollow">http://replicaisland.blogspot.co.il/2010/11/building-reflective-object-system-in-c.html</a></p>\n\n<p>the article explains in great detail how you can implement a pretty simple and rudimentary reflection system. granted its not the most wholesome solution, and there are rough edges left to be sorted out but for my needs it was sufficient.</p>\n\n<p>the bottom line - reflection can pay off if done correctly, and it is completely feasible in c++.</p>\n'}, {'answer_id': 15848342, 'author': 'Russell Standish', 'author_id': 2251673, 'author_profile': 'https://Stackoverflow.com/users/2251673', 'pm_score': 2, 'selected': False, 'text': '<p>Check out Classdesc <a href="http://classdesc.sf.net" rel="nofollow">http://classdesc.sf.net</a>. It provides reflection in the form of class "descriptors", works with any standard C++ compiler (yes it is known to work with Visual Studio as well as GCC), and does not require source code annotation (although some pragmas exist to handle tricky situations). It has been in development for more than a decade, and used in a number of industrial scale projects.</p>\n'}, {'answer_id': 19658761, 'author': 'Germán Diago', 'author_id': 429879, 'author_profile': 'https://Stackoverflow.com/users/429879', 'pm_score': 3, 'selected': False, 'text': '<p>EDIT: Updated broken link as of February, the 7th, 2017.</p>\n\n<p>I think noone mentioned this:</p>\n\n<p>At CERN they use a full reflection system for C++:</p>\n\n<p><a href="https://root.cern.ch/how/how-use-reflex" rel="nofollow noreferrer">CERN Reflex</a>. It seems to work very well.</p>\n'}, {'answer_id': 22351312, 'author': 'Eugene G', 'author_id': 2326852, 'author_profile': 'https://Stackoverflow.com/users/2326852', 'pm_score': 1, 'selected': False, 'text': '<p>I would like to advertise the existence of the automatic introspection/reflection toolkit "IDK". It uses a meta-compiler like Qt\'s and adds meta information directly into object files. It is claimed to be easy to use. No external dependencies. It even allows you to automatically reflect std::string and then use it in scripts. Please look at <a href="https://sites.google.com/site/idevkit" rel="nofollow">IDK</a></p>\n'}, {'answer_id': 24918266, 'author': 'Zack', 'author_id': 1225169, 'author_profile': 'https://Stackoverflow.com/users/1225169', 'pm_score': 4, 'selected': False, 'text': '<p>There is another new library for reflection in C++, called <a href="http://www.rttr.org" rel="noreferrer">RTTR</a> (Run Time Type Reflection, see also <a href="https://github.com/rttrorg/rttr" rel="noreferrer">github</a>).</p>\n\n<p>The interface is similar to reflection in C# and it works without any RTTI.</p>\n'}, {'answer_id': 34551440, 'author': 'Nick', 'author_id': 3233, 'author_profile': 'https://Stackoverflow.com/users/3233', 'pm_score': 3, 'selected': True, 'text': '<p><a href="http://billyquith.github.io/ponder/" rel="nofollow">Ponder</a> is a C++ reflection library, in answer to this question. I considered the options and decided to make my own since I couldn\'t find one that ticked all my boxes. </p>\n\n<p>Although there are great answers to this question, I don\'t want to use tonnes of macros, or rely on Boost. Boost is a great library, but there are lots of small bespoke C++0x projects out that are simpler and have faster compile times. There are also advantages to being able to decorate a class externally, like wrapping a C++ library that doesn\'t (yet?) support C++11. It is fork of CAMP, using C++11, that <em>no longer requires Boost</em>.</p>\n'}, {'answer_id': 40476588, 'author': 'jenkas', 'author_id': 2604941, 'author_profile': 'https://Stackoverflow.com/users/2604941', 'pm_score': 1, 'selected': False, 'text': '<p>Reflection in C++ is very useful, in cases there you need to run some method for each member(For example: serialization, hashing, compare). I came with generic solution, with very simple syntax:</p>\n\n<pre><code>struct S1\n{\n    ENUMERATE_MEMBERS(str,i);\n    std::string str;\n    int i;\n};\nstruct S2\n{\n    ENUMERATE_MEMBERS(s1,i2);\n    S1 s1;\n    int i2;\n};\n</code></pre>\n\n<p>Where ENUMERATE_MEMBERS is a macro, which is described later(UPDATE):</p>\n\n<p>Assume we have defined serialization function for int and std::string like this:</p>\n\n<pre><code>void EnumerateWith(BinaryWriter &amp; writer, int val)\n{\n    //store integer\n    writer.WriteBuffer(&amp;val, sizeof(int));\n}\nvoid EnumerateWith(BinaryWriter &amp; writer, std::string val)\n{\n    //store string\n    writer.WriteBuffer(val.c_str(), val.size());\n}\n</code></pre>\n\n<p>And we have generic function near the "secret macro" ;)</p>\n\n<pre><code>template&lt;typename TWriter, typename T&gt;\nauto EnumerateWith(TWriter &amp;&amp; writer, T &amp;&amp; val) -&gt; is_enumerable_t&lt;T&gt;\n{\n    val.EnumerateWith(write); //method generated by ENUMERATE_MEMBERS macro\n}\n</code></pre>\n\n<p>Now you can write</p>\n\n<pre><code>S1 s1;\nS2 s2;\n//....\nBinaryWriter writer("serialized.bin");\n\nEnumerateWith(writer, s1); //this will call EnumerateWith for all members of S1\nEnumerateWith(writer, s2); //this will call EnumerateWith for all members of S2 and S2::s1 (recursively)\n</code></pre>\n\n<p>So having ENUMERATE_MEMBERS macro in struct definition, you can build serialization, compare, hashing, and other stuffs without touching original type, the only requirement is to implement "EnumerateWith" method for each type, which is not enumerable, per enumerator(like BinaryWriter). Usually you will have to implement 10-20 "simple" types to support any type in your project.</p>\n\n<p>This macro should have zero-overhead to struct creation/destruction in run-time, and the code of T.EnumerateWith() should be generated on-demand, which can be achieved by making it template-inline function, so the only overhead in all the story is to add ENUMERATE_MEMBERS(m1,m2,m3...) to each struct, while implementing specific method per member type is a must in any solution, so I do not assume it as overhead.</p>\n\n<p>UPDATE: \nThere is very simple implementation of ENUMERATE_MEMBERS macro(however it could be a little be extended to support inheritance from enumerable struct)</p>\n\n<pre><code>#define ENUMERATE_MEMBERS(...) \\\ntemplate&lt;typename TEnumerator&gt; inline void EnumerateWith(TEnumerator &amp; enumerator) const { EnumerateWithHelper(enumerator, __VA_ARGS__ ); }\\\ntemplate&lt;typename TEnumerator&gt; inline void EnumerateWith(TEnumerator &amp; enumerator) { EnumerateWithHelper(enumerator, __VA_ARGS__); }\n\n// EnumerateWithHelper\ntemplate&lt;typename TEnumerator, typename ...T&gt; inline void EnumerateWithHelper(TEnumerator &amp; enumerator, T &amp;...v) \n{ \n    int x[] = { (EnumerateWith(enumerator, v), 1)... }; \n}\n\n// Generic EnumerateWith\ntemplate&lt;typename TEnumerator, typename T&gt;\nauto EnumerateWith(TEnumerator &amp; enumerator, T &amp; val) -&gt; std::void_t&lt;decltype(val.EnumerateWith(enumerator))&gt;\n{\n    val.EnumerateWith(enumerator);\n}\n</code></pre>\n\n<p>And you do not need any 3rd party library for these 15 lines of code ;)</p>\n'}, {'answer_id': 54973430, 'author': 'S.S. Anne', 'author_id': 10795151, 'author_profile': 'https://Stackoverflow.com/users/10795151', 'pm_score': 0, 'selected': False, 'text': '<p>If you declare a pointer to a function like this:</p>\n\n<pre><code>int (*func)(int a, int b);\n</code></pre>\n\n<p>You can assign a place in memory to that function like this (requires <code>libdl</code> and <code>dlopen</code>)</p>\n\n<pre><code>#include &lt;dlfcn.h&gt;\n\nint main(void)\n{\n    void *handle;\n    char *func_name = "bla_bla_bla";\n    handle = dlopen("foo.so", RTLD_LAZY);\n    *(void **)(&amp;func) = dlsym(handle, func_name);\n    return func(1,2);\n}\n</code></pre>\n\n<p>To load a local symbol using indirection, you can use <code>dlopen</code> on the calling binary (<code>argv[0]</code>).</p>\n\n<p>The only requirement for this (other than <code>dlopen()</code>, <code>libdl</code>, and <code>dlfcn.h</code>) is knowing the arguments and type of the function.</p>\n'}, {'answer_id': 55364085, 'author': 'TarmoPikaro', 'author_id': 2338477, 'author_profile': 'https://Stackoverflow.com/users/2338477', 'pm_score': 2, 'selected': False, 'text': '<p>If you\'re looking for relatively simple C++ reflection - I have collected from various sources macro / defines, and commented them out how they works. You can download header \nfiles from here:</p>\n\n<p><a href="https://github.com/tapika/TestCppReflect/blob/master/MacroHelpers.h" rel="nofollow noreferrer">https://github.com/tapika/TestCppReflect/blob/master/MacroHelpers.h</a></p>\n\n<p>set of defines, plus functionality on top of it:</p>\n\n<p><a href="https://github.com/tapika/TestCppReflect/blob/master/CppReflect.h" rel="nofollow noreferrer">https://github.com/tapika/TestCppReflect/blob/master/CppReflect.h</a>\n<a href="https://github.com/tapika/TestCppReflect/blob/master/CppReflect.cpp" rel="nofollow noreferrer">https://github.com/tapika/TestCppReflect/blob/master/CppReflect.cpp</a>\n<a href="https://github.com/tapika/TestCppReflect/blob/master/TypeTraits.h" rel="nofollow noreferrer">https://github.com/tapika/TestCppReflect/blob/master/TypeTraits.h</a></p>\n\n<p>Sample application resides in git repository as well, in here:\n<a href="https://github.com/tapika/TestCppReflect/" rel="nofollow noreferrer">https://github.com/tapika/TestCppReflect/</a></p>\n\n<p>I\'ll partly copy it here with explanation:</p>\n\n<pre><code>#include "CppReflect.h"\nusing namespace std;\n\n\nclass Person\n{\npublic:\n\n    // Repack your code into REFLECTABLE macro, in (&lt;C++ Type&gt;) &lt;Field name&gt;\n    // form , like this:\n\n    REFLECTABLE( Person,\n        (CString)   name,\n        (int)       age,\n...\n    )\n};\n\nvoid main(void)\n{\n    Person p;\n    p.name = L"Roger";\n    p.age = 37;\n...\n\n    // And here you can convert your class contents into xml form:\n\n    CStringW xml = ToXML( &amp;p );\n    CStringW errors;\n\n    People ppl2;\n\n    // And here you convert from xml back to class:\n\n    FromXml( &amp;ppl2, xml, errors );\n    CStringA xml2 = ToXML( &amp;ppl2 );\n    printf( xml2 );\n\n}\n</code></pre>\n\n<p><code>REFLECTABLE</code> define uses class name + field name with <code>offsetof</code> - to identify at which place in memory particular field is located. I have tried to pick up .NET terminology for as far as possible, but C++ and C# are different, so it\'s not 1 to 1. Whole C++ reflection model resides in <code>TypeInfo</code> and <code>FieldInfo</code> classes.</p>\n\n<p>I have used pugi xml parser to fetch demo code into xml and restore it back from xml.</p>\n\n<p>So output produced by demo code looks like this:</p>\n\n<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;\n&lt;People groupName="Group1"&gt;\n    &lt;people&gt;\n        &lt;Person name="Roger" age="37" /&gt;\n        &lt;Person name="Alice" age="27" /&gt;\n        &lt;Person name="Cindy" age="17" /&gt;\n    &lt;/people&gt;\n&lt;/People&gt;\n</code></pre>\n\n<p>It\'s also possible to enable any 3-rd party class / structure support via TypeTraits class, and partial template specification - to define your own TypeTraitsT class, in similar manner to CString or int - see example code in</p>\n\n<p><a href="https://github.com/tapika/TestCppReflect/blob/master/TypeTraits.h#L195" rel="nofollow noreferrer">https://github.com/tapika/TestCppReflect/blob/master/TypeTraits.h#L195</a></p>\n\n<p>This solution is applicable for Windows / Visual studio. It\'s possible to port it to other OS/compilers, but haven\'t done that one. (Ask me if you really like solution, I might be able to help you out)</p>\n\n<p>This solution is applicable for one shot serialization of one class with multiple subclasses.</p>\n\n<p>If you however are searching for mechanism to serialize class parts or even to control what functionality reflection calls produce, you could take a look on following solution:</p>\n\n<p><a href="https://github.com/tapika/cppscriptcore/tree/master/SolutionProjectModel" rel="nofollow noreferrer">https://github.com/tapika/cppscriptcore/tree/master/SolutionProjectModel</a></p>\n\n<p>More detailed information can be found from youtube video:</p>\n\n<p>C++ Runtime Type Reflection\n<a href="https://youtu.be/TN8tJijkeFE" rel="nofollow noreferrer">https://youtu.be/TN8tJijkeFE</a></p>\n\n<p>I\'m trying to explain bit deeper on how c++ reflection will work.</p>\n\n<p>Sample code will look like for example this:</p>\n\n<p><a href="https://github.com/tapika/cppscriptcore/blob/master/SolutionProjectModel/testCppApp.cpp" rel="nofollow noreferrer">https://github.com/tapika/cppscriptcore/blob/master/SolutionProjectModel/testCppApp.cpp</a></p>\n\n<pre><code>c.General.IntDir = LR"(obj\\$(ProjectName)_$(Configuration)_$(Platform)\\)";\nc.General.OutDir = LR"(bin\\$(Configuration)_$(Platform)\\)";\nc.General.UseDebugLibraries = true;\nc.General.LinkIncremental = true;\nc.CCpp.Optimization = optimization_Disabled;\nc.Linker.System.SubSystem = subsystem_Console;\nc.Linker.Debugging.GenerateDebugInformation = debuginfo_true;\n</code></pre>\n\n<p>But each step here actually results in function call \nUsing C++ properties with <code>__declspec(property(get =, put ... )</code>.</p>\n\n<p>which receives full information on C++ Data Types, C++ property names and class instance pointers, in form of path, and based on that information you can generate xml, json or even serialize that one over internet.</p>\n\n<p>Examples of such virtual callback functions can be found here:</p>\n\n<p><a href="https://github.com/tapika/cppscriptcore/blob/master/SolutionProjectModel/VCConfiguration.cpp" rel="nofollow noreferrer">https://github.com/tapika/cppscriptcore/blob/master/SolutionProjectModel/VCConfiguration.cpp</a></p>\n\n<p>See functions <code>ReflectCopy</code>, and virtual function <code>::OnAfterSetProperty</code>.</p>\n\n<p>But since topic is really advanced - I recommend to check through video first.</p>\n\n<p>If you have some improvement ideas, feel free to contact me.</p>\n'}, {'answer_id': 58131045, 'author': 'nnolte', 'author_id': 7014926, 'author_profile': 'https://Stackoverflow.com/users/7014926', 'pm_score': 1, 'selected': False, 'text': '<p>You can achieve cool static reflection features for structs with <a href="http://boostorg.github.io/hana/group__group-Struct.html#gab9efb238a82207d91643994c5295cf8c" rel="nofollow noreferrer">BOOST_HANA_DEFINE_STRUCT</a> from the Boost::Hana library.<br>\nHana is quite versatile, not only for the usecase you have in mind but for a lot of template metaprogramming.</p>\n'}, {'answer_id': 59447720, 'author': 'TheNitesWhoSay', 'author_id': 12580603, 'author_profile': 'https://Stackoverflow.com/users/12580603', 'pm_score': 3, 'selected': False, 'text': '<p>The <a href="https://github.com/TheNitesWhoSay/RareCpp" rel="nofollow noreferrer">RareCpp</a> library makes for fairly easy and intuitive reflection - all field/type information is designed to either be available in arrays or to feel like array access. It\'s written for C++17 and works with Visual Studios, g++, and Clang. The library is header only, meaning you need only copy &quot;Reflect.h&quot; into your project to use it.</p>\n<p>Reflected structs or classes need the REFLECT macro, where you supply the name of the class you\'re reflecting and the names of the fields.</p>\n<pre><code>class FuelTank {\n    public:\n        float capacity;\n        float currentLevel;\n        float tickMarks[2];\n\n    REFLECT(FuelTank, capacity, currentLevel, tickMarks)\n};\n</code></pre>\n<p>That\'s all there is, no additional code is needed to setup reflection. Optionally you can supply class and field annotations to be able to traverse superclasses or add additional compile-time information to a field (such as Json::Ignore).</p>\n<p>Looping through fields can be as simple as...</p>\n<pre><code>for ( size_t i=0; i&lt;FuelTank::Class::TotalFields; i++ )\n    std::cout &lt;&lt; FuelTank::Class::Fields[i].name &lt;&lt; std::endl;\n</code></pre>\n<p>You can loop through an object instance to access field values (which you can read or modify) and field type information...</p>\n<pre><code>FuelTank::Class::ForEachField(fuelTank, [&amp;](auto &amp; field, auto &amp; value) {\n    using Type = typename std::remove_reference&lt;decltype(value)&gt;::type;\n    std::cout &lt;&lt; TypeToStr&lt;Type&gt;() &lt;&lt; &quot; &quot; &lt;&lt; field.name &lt;&lt; &quot;: &quot; &lt;&lt; value &lt;&lt; std::endl;\n});\n</code></pre>\n<hr />\n<p>A <a href="https://github.com/TheNitesWhoSay/RareCpp/blob/master/JSON.md" rel="nofollow noreferrer">JSON Library</a> is built on top of RandomAccessReflection which auto identifies appropriate JSON output representations for reading or writing, and can recursively traverse any reflected fields, as well as arrays and STL containers.</p>\n<pre><code>struct MyOtherObject { int myOtherInt; REFLECT(MyOtherObject, myOtherInt) };\nstruct MyObject\n{\n    int myInt;\n    std::string myString;\n    MyOtherObject myOtherObject;\n    std::vector&lt;int&gt; myIntCollection;\n\n    REFLECT(MyObject, myInt, myString, myOtherObject, myIntCollection)\n};\n\nint main()\n{\n    MyObject myObject = {};\n    std::cout &lt;&lt; &quot;Enter MyObject:&quot; &lt;&lt; std::endl;\n    std::cin &gt;&gt; Json::in(myObject);\n    std::cout &lt;&lt; std::endl &lt;&lt; std::endl &lt;&lt; &quot;You entered:&quot; &lt;&lt; std::endl;\n    std::cout &lt;&lt; Json::pretty(myObject);\n}\n</code></pre>\n<p>The above could be ran like so...</p>\n<pre><code>Enter MyObject:\n{\n  &quot;myInt&quot;: 1337, &quot;myString&quot;: &quot;stringy&quot;, &quot;myIntCollection&quot;: [2,4,6],\n  &quot;myOtherObject&quot;: {\n    &quot;myOtherInt&quot;: 9001\n  }\n}\n\n\nYou entered:\n{\n  &quot;myInt&quot;: 1337,\n  &quot;myString&quot;: &quot;stringy&quot;,\n  &quot;myOtherObject&quot;: {\n    &quot;myOtherInt&quot;: 9001\n  },\n  &quot;myIntCollection&quot;: [ 2, 4, 6 ]\n}\n</code></pre>\n<p>See also...</p>\n<ul>\n<li><a href="https://github.com/TheNitesWhoSay/RareCpp/blob/master/README.md" rel="nofollow noreferrer">Reflect Documentation</a></li>\n<li><a href="https://github.com/TheNitesWhoSay/RareCpp/blob/master/RareCppLib/Reflect.h" rel="nofollow noreferrer">Reflect Implementation</a></li>\n<li><a href="https://github.com/TheNitesWhoSay/RareCpp/blob/master/RareCpp/Main.cpp" rel="nofollow noreferrer">More Usage Examples</a></li>\n</ul>\n'}]
['https://Stackoverflow.com/questions/41453', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3233/']