14

您在 C++ 中看到的最酷的元编程示例是什么?
您在 C++ 中看到过哪些元编程的实际用途?

4

10 回答 10

24

就个人而言,我认为Boost.Spirit是元编程的一个非常了不起的例子。它是一个完整的解析器生成器,可让您使用 C++ 语法表达语法。

于 2008-10-26T03:13:11.370 回答
21

元编程最实际的用途是将运行时错误转化为编译时错误。

示例:让我们调用接口 IFoo。我的一个程序处理了一个 COM 对象,该对象具有多个 IFoo 路径(非常复杂的继承层次结构)。不幸的是,底层的 COM 对象实现没有意识到它们有多个到 IFoo 的路径。他们认为它总是最左边的那个。所以在他们的代码中,下面的模式很常见

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

第二个 IFoo 虽然导致生成的“p”指针完全无效(多重继承很危险)。

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.

于 2008-10-26T03:47:48.853 回答
15

Not of practical usage (except maybe for compiler testing), but metatrace is a Whitted-Style (i.e. recursive and deterministic) ray tracer which generates images like those at compilation time:

metatrace example

Some more complex parts of the code can be seen in fixp.hh, which has an implementation of fixed-point sqrt using the Heron method, or sphere.hh which shows ray/sphere-intersection calculation.

于 2011-07-26T12:25:22.003 回答
12

Blitz++ does some impressive things with templates (for instance, a single readable line of code can be turned into a set of loops over a multidimensional array, automatically optimized for the best traversal order).

于 2008-10-26T03:52:36.173 回答
8

最酷的元编程示例:欺骗编译器计算素数列表。不是很实用,但令人印象深刻。

一种实际用途是编译时断言语句,即如果布尔条件不成立,则会导致编译错误。

于 2008-10-26T02:54:16.260 回答
8

Loki written by Andrei Alexandrescu

于 2008-10-26T14:09:35.633 回答
4

I would have to say Boost.Lambda, Boost.Function, and Boost.Bind and the way that they all work seamlessly together. They provide a really slick interface and make functional programming about as easy as possible in a language that wasn't really built for it.

于 2008-10-26T04:27:27.010 回答
2

luabind 是一个非常酷的实用示例,一个非常好的绑定 dsl,用于将 C++ 类绑定到 lua

于 2008-10-26T03:28:45.707 回答
1

BOOST_FOREACH

Static assertion (boosts version here)

(Note: builtin support for range-based for loops and static assertions is introduced in C++11)

于 2010-10-26T21:44:11.483 回答
0

I posed a question not to long ago: C++ Runtime Knowledge of Classes and the answer I got back from a StackOverflow user "Denice" was a URL to a website Meatspace: C++ runtime class registration.

I think that is a really cool way to use templates and instantiate objects that are all derived from a base class, so that when I have 10 C++ files, they can all just add the AUTO_REGISTER_BASE() at the bottom, and when everything is all done and linked, only those classes/files that made it would be registered, so at runtime you can switch between the different classes that are available, and those that are not available are not registered and thus can't accidently be called.

There are many different OS dependant ways to do event notification (select(), kqueue(), /dev/epoll, Solaris has it's own thing, poll()), and I needed a way to have all of the class files exist in the directory, but depending on what OS the Makefile was run, it would only compile certain ones. I needed a way to know at runtime which ones were available, and have a way for the programmer using the library to select their preference, however if it was unavailable to just use the one that made the most logical sense for the platform (they each have weights assigned to them).

The code above helped me accomplish this goal, with some hefty modifications, but it helped me none-the-less!

于 2008-10-26T05:17:45.363 回答