Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the coolest examples of metaprogramming that you've seen in C++? [closed]

What are the coolest examples of metaprogramming that you've seen in C++?
What are some practical uses of metaprogramming that you've seen in C++?

like image 876
Brian R. Bondy Avatar asked Oct 26 '08 02:10

Brian R. Bondy


People also ask

What is metaprogramming used for?

Metaprogramming can be used to move computations from run-time to compile-time, to generate code using compile time computations, and to enable self-modifying code. The ability of a programming language to be its own metalanguage is called reflection.

What is a metaprogramming language?

Metaprogramming is a programming technique in which computer programs have the ability to treat other programs as their data. This means that a program can be designed to read, generate, analyze, or transform other programs, and even modify itself while running.

Are generics metaprogramming?

Generics are used for metaprogramming in Ada. They are useful for abstract algorithms that share common properties with each other.

What is metaprogramming in Java?

Metaprogramming is writing programs that manipulate other programs or themselves based on metadata.


2 Answers

Personally, I think Boost.Spirit is a pretty amazing example of meta-programming. It's a complete parser generator that lets you express grammars using C++ syntax.

like image 53
Ferruccio Avatar answered Oct 09 '22 10:10

Ferruccio


The most practical use of meta programming is turning a runtime error into a compile time error.

Example: Lets call the interface IFoo. One of my programs dealt with a COM object that had multiple paths to IFoo (very complicated inheritance hierarchy). Unfortunately the underlying COM object implementation didn't realize they had multiple paths to IFoo. They assumed it was always the left most one. So inside their code, the following pattern was very common

   void SomeMethod(IFoo* pFoo) {
        CFooImpl *p = (CFooImpl)pFoo;
   }

The second IFoo though caused the resulting "p" pointer to be completely invalid (multiple inheritance is dangerous).

The long term solution was to have the COM object owner fix this issue. Short term though I needed to make sure that I always returned the correct IFoo. I could guarantee that I had the appropriate IFoo by using a QI and avoiding any implicit casts to IFoo. So I created a new CComPtr<> implementation and added the following override to the equal method.

template <typename T>
CComPtr<T>& operator=(const T* pT)  { 
// CComPTr Assign logic
}
template <>
CComPtr<IFoo> operator=<IFoo>(const IFoo* pT) {
  COMPILE_ERROR();
}

This quickly revealed every single place I implicitly casted to IFoo.

like image 29
JaredPar Avatar answered Oct 09 '22 11:10

JaredPar