I have just started my journey as a C++ developer, and I have recently been researching a topic called meta-programming. So far, I have gone through a lot of information on the topic, and have few questions.

  1. Usage of Reflection in c++: Is it possible? As per my understanding reflection is a meta-programming technique which has the ability to manipulate a state of a program, and its manipulation can be introspection or introcession.

  2. What is the difference between reflective architecture and meta-level architecture? In which category does meta-programming architecture fall in?

  3. Meta-programming comes in two levels: meta level and base level. In my understanding, the object program or the system under investigation is the base level, and the meta program that is going to operate over the system under investigation is the meta level. Am I correct?

Also help me by providing some guidelines about working on template metaprogramming in C++.
May you suggest me some workable examples I can look for to get hands-on in this programming technique?

  • 2
    This question might belong more on programmers than on stackoverflow. The former being more theoretical, and the later more practical. Some parts can be easily answered from a practical point of view C++ does not have reflection, but others are not code related (difference between reflective and metalevel architectures, for example) Commented Jun 19, 2011 at 11:44
  • This SO answer addresses why C++ does not have reflection: stackoverflow.com/q/359237/120163, and some weak C++ techniques for simulating it badly.
    – Ira Baxter
    Commented Jun 19, 2011 at 13:57
  • If you are very interested into metaprogramming, I suggest you to have a look at D language, which is clearer than C++ on this point.
    – deadalnix
    Commented Jun 19, 2011 at 16:54

2 Answers 2


Metaprogramming simply means that you have the means to write code that writes other code (in a broad sense). C++ does this through the template mechanism, which is basically a type-safe macro language. It is perfectly possible to just apply the template expansion step, and the result will be valid template-free C++ code.

Reflection is the ability of a programming language and / or runtime environment to inspect (and often modify) its own language elements at run-time. Typically, reflective languages offer data structures that represent language elements like classes, methods, variables, parameters, etc., as well as routines or classes to inspect, modify, and create them. The modification ability makes reflection a type of meta-programming, since it allows you to write code that generates other code.

Other examples of metaprogramming include code generators, self-modifying binaries (though these aren't as popular anymore today as they were two decades ago), C macros; one could argue that currying and similar functional-programming techniques are also a kind of metaprogramming.

  • +1 I really like your answer and I want to ask about using compiler as a service like in C# or using a scripting engine like in Java to execute code that is constructed in run-time. Isn't this also a form of metaprogramming?
    – M.Sameer
    Commented Jun 19, 2011 at 14:41
  • So anyone who builds a Python interpreter in C is metaprogramming? What about the implementation of an ORM that maps an object oriented language to SQL? Is every web framework an example of metaprogramming?
    – kojiro
    Commented Jun 19, 2011 at 17:02
  1. No. It isn't. C++ programs are compiled to machine code and all information about classes etc. are lost (unless using C++/CLI for the .NET environment, but that's not pure C++).

2-3. I don't know - wait for another answer that helps with that :-)

Meta programming using C++ templates is very powerful, but also complex. The template expansion requires functional style of programming rather than imperative programming that C++ uses. If you don't have previous experience with functional programming I would recommend you to make detour and learn som basic lisp, haskell or scheme.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.