Search code examples
c++metaprogrammingc++14constexprtypeinfo

Printing types including decorations, template metaprogramming, constexpr, what to use?


I have a Trait like this specialized for functions:

template <class Ret, class...Args>
struct FunctionTraits<Ret (*)(Args...)> {
   using ReturnType = Ret;

   template <std::size_t>
   using Parameter = std::tuple_element_t<std::tuple<Args...>>;
};

Now I want to print the signature of the function without losing the decorators.

For that I implemented a metafunction like this:

template <class T>
struct GetTypeInfoString {};

It is specialized for every undecorated type, but I also want to print the decorated types. I am using it like this:

extern constexpr auto intstr = makeStringLiteral("int");

template <>
struct GetTypeInfoString<int> {
   static constexprt auto & value = intstr;
};

Now I already have the basic information, I want to implement a constexpr function:

template <class T>
constexpr const char * getTypeInfo() {
    //Something here...
}

My goal is that I print the type with the decorations, not just the basic type. Namely: int const * [][3], etc...


Solution

  • The question is basically how to get this:

    int main()
    {
        std::cout << TypeInfo<const int>::value() << std::endl;
        std::cout << TypeInfo<const int&>::value() << std::endl;
        std::cout << TypeInfo<int&&>::value() << std::endl;
        std::cout << TypeInfo<const volatile int&>::value() << std::endl;
    }
    

    To produce this:

    const int
    const int&
    int&&
    const volatile int&
    

    In a constexpr manner.

    Answer:

    #include <iostream>
    #include <tuple>
    
    template <class T>
    struct TypeInfo;
    
    template<std::size_t N>
    struct immutable_string
    {
        constexpr immutable_string(const char (&s)[N])
        : _data {}
        {
            for (std::size_t i = 0 ; i < N ; ++i)
                _data[i] = s[i];
        }
    
        constexpr immutable_string()
        : _data {}
        {
        }
    
        constexpr char& operator[](std::size_t i) { return _data[i]; }
        constexpr const char& operator[](std::size_t i) const { return _data[i]; }
    
        using ref = const char (&)[N];
    
        constexpr ref data() const { return _data; }
        static constexpr std::size_t size() { return N-1; }
    
        char _data[N];
    };
    
    template<std::size_t N>
    std::ostream& operator<<(std::ostream& os, immutable_string<N> s)
    {
        return os.write(s.data(), s.size());
    }
    
    template<std::size_t LN, std::size_t RN>
    constexpr auto operator+(immutable_string<LN> l, immutable_string<RN> r)
    {
        constexpr std::size_t len = LN + RN - 2;
        immutable_string<len + 1> result;
        std::size_t i = 0;
        for ( ; i < (LN-1) ; ++i)
        {
            result[i] = l[i];
        }
        for (auto j = 0 ; j < (RN-1) ; ++j)
        {
            result[i + j] = r[j];
        }
    
        return result;
    }
    
    template<std::size_t N>
    constexpr auto literal(const char (&s)[N])
    {
        return immutable_string<N>(s);
    }
    
    template <>
    struct TypeInfo<int> {
        static constexpr auto value() { return literal("int"); }
    };
    
    template<class T>
    struct TypeInfo<const T>
    {
        static constexpr auto value() { return literal("const ") + TypeInfo<T>::value(); }
    };
    
    template<class T>
    struct TypeInfo<volatile T>
    {
        static constexpr auto value() { return literal("volatile ") + TypeInfo<T>::value(); }
    };
    
    template<class T>
    struct TypeInfo<const volatile T>
    {
        static constexpr auto value() { return literal("const volatile ") + TypeInfo<T>::value(); }
    };
    
    template<class T>
    struct TypeInfo<T&>
    {
        static constexpr auto value() { return TypeInfo<T>::value() + literal("&"); }
    };
    
    template<class T>
    struct TypeInfo<T&&>
    {
        static constexpr auto value() { return TypeInfo<T>::value() + literal("&&"); }
    };
    
    
    int main()
    {
        std::cout << TypeInfo<const int>::value() << std::endl;
        std::cout << TypeInfo<const int&>::value() << std::endl;
        std::cout << TypeInfo<int&&>::value() << std::endl;
        std::cout << TypeInfo<const volatile int&>::value() << std::endl;
    }
    

    Produces output:

    const int
    const int&
    int&&
    const volatile int&
    

    Update:

    A more complete/robust example:

    #include <iostream>
    
    template <class T>
    struct TypeInfo;
    
    
    template<std::size_t N>
    struct immutable_string
    {
        constexpr immutable_string(const char (&s)[N])
        : _data {}
        {
            for (std::size_t i = 0 ; i < N ; ++i)
                _data[i] = s[i];
        }
    
        constexpr immutable_string()
        : _data {}
        {
        }
    
        constexpr char& operator[](std::size_t i) { return _data[i]; }
        constexpr const char& operator[](std::size_t i) const { return _data[i]; }
    
        using ref = const char (&)[N];
    
        constexpr ref data() const { return _data; }
        static constexpr std::size_t size() { return N-1; }
    
        char _data[N];
    };
    
    template<std::size_t N>
    std::ostream& operator<<(std::ostream& os, immutable_string<N> s)
    {
        return os.write(s.data(), s.size());
    }
    
    template<std::size_t LN, std::size_t RN>
    constexpr auto operator+(immutable_string<LN> l, immutable_string<RN> r)
    {
        constexpr std::size_t len = LN + RN - 2;
        immutable_string<len + 1> result;
        std::size_t i = 0;
        for ( ; i < (LN-1) ; ++i)
        {
            result[i] = l[i];
        }
        for (auto j = 0 ; j < (RN-1) ; ++j)
        {
            result[i + j] = r[j];
        }
    
        return result;
    }
    
    template<std::size_t N>
    constexpr auto literal(const char (&s)[N])
    {
        return immutable_string<N>(s);
    }
    
    template <>
    struct TypeInfo<int> {
        static constexpr auto value() { return literal("int"); }
    };
    
    template<class T>
    struct TypeInfo<const T>
    {
        static constexpr auto value() { return TypeInfo<T>::value() + literal(" const"); }
    };
    
    template<class T>
    struct TypeInfo<volatile T>
    {
        static constexpr auto value() { return TypeInfo<T>::value() + literal(" volatile"); }
    };
    
    template<class T>
    struct TypeInfo<const volatile T>
    {
        static constexpr auto value() { return TypeInfo<T>::value() + literal(" const volatile"); }
    };
    
    template<class T>
    struct TypeInfo<T&>
    {
        static constexpr auto value() { return TypeInfo<T>::value() + literal("&"); }
    };
    
    template<class T>
    struct TypeInfo<T&&>
    {
        static constexpr auto value() { return TypeInfo<T>::value() + literal("&&"); }
    };
    
    template<class T>
    struct TypeInfo<T*>
    {
        static constexpr auto value() { return TypeInfo<T>::value() + literal("*"); }
    };
    
    
    int main()
    {
        std::cout << TypeInfo<const int>::value() << std::endl;
        std::cout << TypeInfo<const int&>::value() << std::endl;
        std::cout << TypeInfo<int&&>::value() << std::endl;
        std::cout << TypeInfo<const volatile int&>::value() << std::endl;
        std::cout << TypeInfo<const volatile int* const* volatile * const volatile **const *&>::value() << std::endl;
    }
    

    expected output:

    int const
    int const&
    int&&
    int const volatile&
    int const volatile* const* volatile* const volatile** const*&