Conversion Summary


In this chapter, you have learned about the Boost.Conversion library, starting with polymorphic_cast. The rationale for polymorphic_cast is code clarity and safetyclarity, because it gives us increased flexibility in stating our intent in code, and safety, because it's safer than its companion dynamic_cast<T*>, because tests of the resulting pointer are easily forgotten.

You then looked at safe optimizations, using polymorphic_downcast, which adds dynamic_cast-like safety in debug builds, but uses static_cast for the conversion. This makes it safer than static_cast alone.

numeric_cast helped with some of the thorny issues related to numeric conversions. Again, code clarity was improved and we stayed clear of both undefined and implementation-defined behavior.

Finally, there was lexical_cast. No more repetitive conversion functions. That's why it's been proposed for inclusion in the next revision of the C++ Standard Library. It is a tool that is very handy for converting different streamable data types.

If you were to read the implementation for these casts, you'd agree that none of them are very complicated. Still, it took insight, vision, and knowledge to recognize the need for them and to implement them correctly, portably, and efficiently. Not all people realize that there is something amiss when using dynamic_cast. Not many know the intricacies of integral type conversion and promotion. The Boost conversion "casts" include all of that knowledge and are well crafted and tested; they are excellent candidates for your use.



    Beyond the C++ Standard Library(c) An Introduction to Boost
    Beyond the C++ Standard Library: An Introduction to Boost
    ISBN: 0321133544
    EAN: 2147483647
    Year: 2006
    Pages: 125

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net