type_traits

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/davied9/article/details/85286426

最近喜欢用编译器搞些事情,记录一下获取类型名和获取基础类型的方式

type_traits.h

#ifndef __TYPE_TRAITS_H_INCLUDED__
#define __TYPE_TRAITS_H_INCLUDED__


#include <complex>

    typedef char               sint8 ;
    typedef unsigned char      uint8 ;
    typedef short              sint16;
    typedef unsigned short     uint16;
    typedef int                sint32;
    typedef unsigned int       uint32;
    typedef long long          sint64;
    typedef unsigned long long uint64;
    typedef long               slong ;
    typedef unsigned long      ulong ;


    template<typename T> struct type_name { };

    template<> struct type_name<float > { static const char* value; };
    template<> struct type_name<double> { static const char* value; };

    template<> struct type_name<sint8 >  { static const char* value; };
    template<> struct type_name<uint8 >  { static const char* value; };

    template<> struct type_name<sint16> { static const char* value; };
    template<> struct type_name<uint16> { static const char* value; };

    template<> struct type_name<sint32> { static const char* value; };
    template<> struct type_name<uint32> { static const char* value; };

    template<> struct type_name<sint64> { static const char* value; };
    template<> struct type_name<uint64> { static const char* value; };

    template<> struct type_name<slong > { static const char* value; };
    template<> struct type_name<ulong > { static const char* value; };

    template<> struct type_name<std::complex<float >> { static const char* value; };
    template<> struct type_name<std::complex<double>> { static const char* value; };

    template<> struct type_name<std::complex<sint8>>  { static const char* value; };
    template<> struct type_name<std::complex<uint8>>  { static const char* value; };

    template<> struct type_name<std::complex<sint16>> { static const char* value; };
    template<> struct type_name<std::complex<uint16>> { static const char* value; };

    template<> struct type_name<std::complex<sint32>> { static const char* value; };
    template<> struct type_name<std::complex<uint32>> { static const char* value; };

    template<> struct type_name<std::complex<sint64>> { static const char* value; };
    template<> struct type_name<std::complex<uint64>> { static const char* value; };

    template<> struct type_name<std::complex<slong >> { static const char* value; };
    template<> struct type_name<std::complex<ulong >> { static const char* value; };


    template<typename T>  struct base_type                   { typedef T  result_type;};
    template<typename eT> struct base_type<std::complex<eT>> { typedef eT result_type;};



namespace Private {

    template<bool, bool, int> struct __long_name_str {}; /*complex, signed, byte size*/
    template<> struct __long_name_str<false, true,  2> { static const char* value; };
    template<> struct __long_name_str<false, true,  4> { static const char* value; };
    template<> struct __long_name_str<false, true,  8> { static const char* value; };
    template<> struct __long_name_str<true,  true,  2> { static const char* value; };
    template<> struct __long_name_str<true,  true,  4> { static const char* value; };
    template<> struct __long_name_str<true,  true,  8> { static const char* value; };
    template<> struct __long_name_str<false, false, 2> { static const char* value; };
    template<> struct __long_name_str<false, false, 4> { static const char* value; };
    template<> struct __long_name_str<false, false, 8> { static const char* value; };
    template<> struct __long_name_str<true,  false, 2> { static const char* value; };
    template<> struct __long_name_str<true,  false, 4> { static const char* value; };
    template<> struct __long_name_str<true,  false, 8> { static const char* value; };

} /*namespace ::Private*/


#endif // __TYPE_TRAITS_H_INCLUDED__

type_traits.cpp


#include "type_traits.h"
namespace Private {

    /*template<bool, bool, int> struct __long_name_str {};*/ /*complex, signed, byte size*/
    const char* __long_name_str<false, true,  2>::value = "slong 16";
    const char* __long_name_str<false, true,  4>::value = "slong 32";
    const char* __long_name_str<false, true,  8>::value = "slong 64";

    const char* __long_name_str<true,  true,  2>::value = "complex slong 16";
    const char* __long_name_str<true,  true,  4>::value = "complex slong 32";
    const char* __long_name_str<true,  true,  8>::value = "complex slong 64";

    const char* __long_name_str<false, false, 2>::value = "ulong 16";
    const char* __long_name_str<false, false, 4>::value = "ulong 32";
    const char* __long_name_str<false, false, 8>::value = "ulong 64";

    const char* __long_name_str<true,  false, 2>::value = "complex ulong 16";
    const char* __long_name_str<true,  false, 4>::value = "complex ulong 32";
    const char* __long_name_str<true,  false, 8>::value = "complex ulong 64";

} /*namespace ::Private*/

    
    const char* type_name<float>::value  = "float";
    const char* type_name<double>::value = "double";

    const char* type_name<sint8>::value  = "sint8";
    const char* type_name<uint8>::value  = "uint8";

    const char* type_name<sint16>::value = "sint16";
    const char* type_name<uint16>::value = "uint16";

    const char* type_name<sint32>::value = "sint32";
    const char* type_name<uint32>::value = "uint32";

    const char* type_name<sint64>::value = "sint64";
    const char* type_name<uint64>::value = "uint64";

    const char* type_name<slong>::value  = Private::__long_name_str<false, true,  sizeof(slong)>::value;
    const char* type_name<ulong>::value  = Private::__long_name_str<false, false, sizeof(ulong)>::value;

    const char* type_name<std::complex<float>>::value  = "complex float";
    const char* type_name<std::complex<double>>::value = "complex double";

    const char* type_name<std::complex<sint8>>::value  = "complex sint8";
    const char* type_name<std::complex<uint8>>::value  = "complex uint8";

    const char* type_name<std::complex<sint16>>::value = "complex sint16";
    const char* type_name<std::complex<uint16>>::value = "complex uint16";

    const char* type_name<std::complex<sint32>>::value = "complex sint32";
    const char* type_name<std::complex<uint32>>::value = "complex uint32";

    const char* type_name<std::complex<sint64>>::value = "complex sint64";
    const char* type_name<std::complex<uint64>>::value = "complex uint64";

    const char* type_name<std::complex<slong>>::value  = Private::__long_name_str<true, true,  sizeof(slong)>::value;
    const char* type_name<std::complex<ulong>>::value  = Private::__long_name_str<true, false, sizeof(ulong)>::value;


猜你喜欢

转载自blog.csdn.net/davied9/article/details/85286426