[Home]CPPTM Answers - Exercise 2-4

BOOST WIKI | RecentChanges | Preferences | Page List | Links List

 // (by Ariel Badichi)
 #include <iostream>
 #include <cstddef>

 template<typename T>
 struct type_descriptor;

 template<typename T>
 struct type_descriptor<T *>
 {
     std::ostream & print(std::ostream & out) const
     {
         return out << type_descriptor<T>() << " *";
     }
 };

 template<typename T>
 struct type_descriptor<T &>
 {
     std::ostream & print(std::ostream & out) const
     {
         return out << type_descriptor<T>() << " &";
     }
 };

 template<typename T, std::size_t N>
 struct type_descriptor<T[N]>
 {
     std::ostream & print(std::ostream & out) const
     {
         return out << type_descriptor<T>() << " [" << N << "]";
     }
 };

 template<typename T>
 struct type_descriptor<const T>
 {
     std::ostream & print(std::ostream & out) const
     {
         return out << type_descriptor<T>() << " const";
     }
 };

 template<typename T>
 struct type_descriptor<volatile T>
 {
     std::ostream & print(std::ostream & out) const
     {
         return out << type_descriptor<T>() << " volatile";
     }
 };

 template<>
 struct type_descriptor<int>
 {
     std::ostream & print(std::ostream & out) const
     {
         return out << "int";
     }
 };

 template<>
 struct type_descriptor<char>
 {
     std::ostream & print(std::ostream & out) const
     {
         return out << "char";
     }
 };

 template<>
 struct type_descriptor<long>
 {
     std::ostream & print(std::ostream & out) const
     {
         return out << "long";
     }
 };

 template<>
 struct type_descriptor<short>
 {
     std::ostream & print(std::ostream & out) const
     {
         return out << "short";
     }
 };

 template<typename T>
 std::ostream & operator<< (std::ostream & out, const type_descriptor<T> & desc)
 {
     return desc.print(out);
 }

 int main()
 {
     std::cout << type_descriptor<int>() << "\n"
               << type_descriptor<char *>() << "\n"
               << type_descriptor<long const * &>() << "\n"
               << type_descriptor<short[10]>() << "\n"
               << type_descriptor<volatile int>() << "\n";

     return 0;
 }

// (by Irfan Zaidi)

The above solution works fine but if you have volatile and const in the same compound type it fails to compile on VC++ 8.0. You need to add const volatile together in partial specialization of template to fix this.

    template <typename T>
    struct type_descriptor<const volatile T>
    {
        std::ostream& print(std::ostream& out) const
        {        
            out << "const volatile " << type_descriptor<T>();
            return out;
        }
    };

  // test this by
  std::cout << type_descriptor<const volatile long *&>() << " " << std::endl;

Also it would be nice to handle * const as well

    template <typename T>
    struct type_descriptor<T* const>
    {
        std::ostream& print(std::ostream& out) const
        {        
            out << type_descriptor<T>() << "* const";
            return out;
        }
    };

    // std::cout << type_descriptor<const volatile long *const&>() << " " << std::endl;

BOOST WIKI | RecentChanges | Preferences | Page List | Links List
Edit text of this page | View other revisions
Last edited January 6, 2008 5:40 pm (diff)
Search:
Disclaimer: This site not officially maintained by Boost Developers