I am adapting the templates in SymbolicCpp. Sometimes my template parameter T is a simple variable such as double and sometimes is itself a template type. I have started to use the typeid() of the T to determine what sort of object I have and therefore the appropriate action within the member functions of the template objects.
There is some helpful information on both run time type information and templates in TheDesignAndEvolutionOfCpp but nothing I have found there which combines the two ideas.
Is there any other reference which discusses the possible pitfalls of the combination? I have not found anything on CeePlusPlus RunTimeTypeInformation in wiki at all.
Sounds like a bad idea. You're violating the encapsulation of the template.
Why do you need to do this?
The code I am using provides a set of templates which provide for the flexible handling of algebraic expressions.
I also wish to build the objects from an input stream e.g. XML, where I need to construct an object of appropriate type in a timely way.
I am committed to providing objects which behave externally via overloaded operators as the algebra would.
-- JohnFletcher
I agree with the other italics guy above - sounds fishy. Without having looked at SymbolicCpp I'd say that output should be handled by overloaded << operator, object creation from streams by some kind of factory. I'm not sure I understand your third point. -- AndrewQueisser
Just that the C++ details should all be hidden from the top level as far as possible.
Sounds like you need traits classes, possibly with template specialization. You definitely don't want RTTI.
Thanks I'll look up those - I have found something in GenerativeProgrammingBook. TraitsTemplates look like providing an answer - Thank You all contributors.
Why is not a good idea to check within a member function what it has been given to do and act appropriately? The reason is that SymbolicCpp assumed all the T's would be numbers in different forms, but the overall structure works with objects of its own internal type so I can do Sum(Complex(Sum(Double)))) and therefore do
z = x + iy
which is valid algebra but symbolic at two levels. The problems of handling this are internal to the object, so don't break encapsulation. RTTI tells me which level I am at within the main object, as I have objects within objects.
What if you need to introduce a new type? Then you must go into your template class and add RTTI identification for X. Thus your template class could not be said to be generic at all.
I would like an answer to my original question - has anyone used it?
In twelve years of C++ I've never had to use RTTI. It's not used in any of the standard libraries as far as I know. That should tell you something about the technique.
Neither had I. It looks to me that some of the material on traits classes and traits templates in GenerativeProgrammingBook is going to help me. The properties of all the objects are known at compile time, and it answers the point above about adding further classes.
Glad that helps. I'd also suggest a good book on STL if you don't have one, since these techniques are used throughout the library. Josuttis (TheCppStandardLibrary) is the best STL book in my opinion.
For why I am doing this (if interested) see http://www.ceac.aston.ac.uk/clifford/paper25/index.htm
-- JohnFletcher
CategoryCpp CategoryCppTemplates
off-topic:
SmalltalkLanguage promotes the result of arithmetic expressions to the appropriate class handling the commuting propery correctly. For example, 3 * 1/2 and 1/2 * 3 both result in 3/2. As I read the VisualWorks and Squeak images, the implementation is highly coupled.
I understand that other languages (Eiffel?) provide generic functions that can handle this sort of situation gracefully. Is there some pattern that can be applied in languages without generic functions to handle cases like this without all the coupling?
The answer to that in the case of CeePlusPlus is PromotionTraits. -- JohnFletcher