对于C++反射,我关心的使用领域包括:模版元编程可以解决的那一部分编译时反射,运行时反射在实现IoC、ORM等中可以让程序员写得更惬意的语法糖衣、DSL,在类Rspec测试和Mock中所需要的对于被测程序非侵入式的信息获取和校验。
从 http://www.garret.ru/cppreflection/docs/reflect.html , 我们可以了解到在C++中达成反射的基本方法:
Approach | Advantages | Disadvantages |
---|---|---|
Parse debugging information |
|
|
Special preprocessor which will parse C++ sources and build class descriptors |
|
|
Create specialized compiler which will support reflection |
|
|
Let programmer to provide this information himself |
|
|