How to determine the type of a function parameter given the type of argument passed to it?

后端 未结 3 2234
借酒劲吻你
借酒劲吻你 2021-02-19 11:31

I need a type trait which will report the type of a functor\'s operator() parameter given the type of the functor and the type of an argument passed to it. Basical

3条回答
  •  感动是毒
    2021-02-19 12:23

        #include 
    
        template< typename PParameter00 = void, typename PParameter01 = void, typename PParameter02 = void, typename PParameter03 = void >
        struct TIdentityParameter // Users need to inherit from it. Add more types as needed.
        {
          typedef PParameter00 TType00;
          typedef PParameter01 TType01;
          typedef PParameter02 TType02;
          typedef PParameter03 TType03;
        };
    
        struct TUserFunctor00 : public TIdentityParameter< float const &, int, void * >
        {
          void operator()( float const &, int, void * );
          // or they can do
          //void operator()( TType00, TType01, TType02 );
        };
    
        struct TUserFunctor01 : public TIdentityParameter< char const *, double >
        {
          void operator()( char const*, double );
          // or they can do
          //void operator()( TType00, TType01 );
        };
    
        template< bool pValue >
        struct TValueBool
        {
          static bool const sValue = pValue;
        };
    
        template< typename PType00, typename PType01 >
        struct TIsSame : public TValueBool< false >
        {
        };
    
        template< typename PType >
        struct TIsSame< PType, PType > : public TValueBool< true >
        {
        };
    
        int main( void )
        {
         std::cout << TIsSame< TUserFunctor00::TType02, void * >::sValue << std::endl;
         std::cout << TIsSame< TUserFunctor01::TType00, double >::sValue << std::endl;
    
         return ( 0 );
        }
    
    Code on [ideone][1]. I don't think it's asking too much from users to inherit from your struct in a pattern explained to them. After all, they want to work with your library. Anyway, maybe it's not what you are looking for.
    

    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    EDIT: Here is something, maybe, a bit closer to the functionality JAred is looking for, but, I understand, the style does not appeal to him. Although, within C++03, I don't see how you can do it differently. Note, you can make TIdentityParameter take, say 16 template arguments to cover 16 possible types. Once again, yes, user has to inherit and specify types. Ideone:

    #include 
    
    struct TOneCrazyStruct
    {
    };
    
    template< typename PParameter00 = TOneCrazyStruct, typename PParameter01 = TOneCrazyStruct, typename PParameter02 = TOneCrazyStruct,
      typename PParameter03 = TOneCrazyStruct, typename PParameter04 = TOneCrazyStruct >
    struct TIdentityParameter //Users will need to inherit from this struct as shown below.
    {
      typedef PParameter00 TType00;
      typedef PParameter01 TType01;
      typedef PParameter02 TType02;
      typedef PParameter03 TType03;
      typedef PParameter04 TType04;
    };
    
    struct TUserFunctor00 : public TIdentityParameter< float const &, int, void *, double >
    {
      void operator()( float const &, int, void * );
      void operator()( double );
    };
    
    template< bool pValue >
    struct TValueBool
    {
      static bool const sValue = pValue;
    };
    
    template< typename PType00, typename PType01 >
    struct TIsSame : public TValueBool< false >
    {
    };
    
    template< typename PType >
    struct TIsSame< PType, PType > : public TValueBool< true >
    {
    };
    
    template< typename PFunctor, typename PParameter >
    struct THasType : public TValueBool<
      TIsSame< typename PFunctor::TType00, PParameter >::sValue || TIsSame< typename PFunctor::TType01, PParameter >::sValue
        || TIsSame< typename PFunctor::TType02, PParameter >::sValue || TIsSame< typename PFunctor::TType03, PParameter >::sValue >
    {
    };
    
    int main( void )
    {
     std::cout << THasType< TUserFunctor00, void * >::sValue << std::endl;
     std::cout << THasType< TUserFunctor00, long double >::sValue << std::endl;
    
     return ( 0 );
     }
    

提交回复
热议问题