October 11th, 2009, 03:26 PM
C++ and reflection
What is the exact reason that C++ lacks reflection? Now I know exactly, what reflection is and how it is achieved in other OOP languages. I know that it is used by the compiler to parse the classes, functions, objects,etc. in a C++ program.
Why does a compiler need to do parsing at all? Pardon my lack of knowledge of inner workings of a compiler....
I have heard that this parsing is useful to get type info at runtime. And I have also heard people say that this parser breaks while working with C++. Why does it happen?
Is intellisense feature -? Auto completion is completely based upon reflection?
Thanks and Regards.
October 11th, 2009, 11:02 PM
Re: C++ and reflection
It's just slow and it takes extra space, as well. But these days there is not much excuse not to have it as you would not need to generate it for all objects anyway.
Originally Posted by bhushan1980
It's not bad for largish objects like you see in many database apps but for small objects it causes problems.
There is RTTI in C++ but if I remember properly it doesn't really give as much info and is not on by default.
By parser I think you mean C#, and it fails because C++ structures don't have the info it needs.
October 12th, 2009, 09:58 AM
Re: C++ and reflection
Well...How did I miss that? Actually I was referring to parser in C# and the autocompletion feature that exists for it (and other IDEs). I am wondering, if it is so hard for MS to provide auto-completion for C++ in VS IDE, how come Visual Assist does it? I have not used it personally but have seen it recommended on CG posts and also I have seen it working on some videos and screenshots. However, it is ironical to find out that it is little hard for MS to develop the feature themselves for their own product, since the past 2 versions VS 2005/2008 and forth coming VS 2010 (which lacks this feature for C++ as well) or overlook it completely, while a third party provides the same for successive versions without any problems....
Parsing and autocompletion are not the only issues that are on my mind. There are a lot of benefits of having reflection support and the list which I need not put down here...At the sametime, I also agree that it is difficult to provide looking at the compile architecture of C++; which is ofcourse highly optimized.
On doing some research, I found out that, what reflection enables a compiler to do is obtain the info for all the classes that are linked in the inheritance list and this list starts from a class in the final output which is an exe or a dll (assembly in .NET) and goes back till the root class. The way the C++ compiler works is highly optimized and hence one of the things it does in favor goes against it providing reflection capabilities. It simply excludes the classes from the exe or dll that were never used. On the other hand a C# compiler provides a guaruntee that the class exists no matter what happens. Thus C# helps parsing its metadata very easy and C++ compiler asks a lot more Qs. So if a parser / reflector (or whatever tool that provides reflection) tries to find a missing class and it does not find one, it fails miserably. There might be work around for this, but I am not aware of any. I read that a simple template instantiation is followed by a large number of subsequent calls to other classes / function templates; hence one can imagine the magnitude of work that has to be done to get the information of all those classes. This is all because, unlike C#, the native C++ applications do not have the CLR facility.
Now there is a counter argument, as "originalfamousjohnsmith" said before, that C++ developers have not really cared about reflection as it unnecessarily increases the resources required any which ever form. But, my counter argument is, why not do some research and provide reflection separately and a switch that helps user decide whether to use it or not? The switch can be integrated in the compiler / linker options or can be defined from the code using various macros. That way, a user has it, if needed.
Also, surprisingly, C++0x standards committee has not got any plans to include it in the forth coming standards. I wonder, if they are trying to make C++ a better language from commercial point of view as againts as an ideal language. And MS, sadly as usual, has been complaining about the difficulty that is faced while providing the same. I saw a video that had demoed the VS 2010 IDE features for C++ developers. Even while the person, who was supposed to be on the team to improve the IDE features that are presented to C++ developers, was demoing the features, atleast half of his claims were refuted by the IDE in front of the masses. I am not mocking or offending anyone by stating this and I am sorry if it sounds like it. One thing that was good, that MS did that needs to be mentioned was the removing of .ncb files; not related to this thread though.
October 12th, 2009, 10:34 AM
Re: C++ and reflection
Not sure, where are you talking about Intellisence, and where about Reflection.
Intellisence: in native C++, it works pretty well in VS2005 and later. In C++/CLI it almost doesn't work, even in VS2100.
About Reflection: if you really need it in C++, use C++/CLI. Native C++ remains highly optimized, and doesn't support such things. I think this is good, because there are still developers who need real-time performance.
Click Here to Expand Forum to Full Width
This a Codeguru.com survey!