c++ template casting

后端 未结 7 1154
伪装坚强ぢ
伪装坚强ぢ 2021-01-06 13:48

I\'m a little lost in how to cast templates. I have a function foo which takes a parameter of type ParamVector*. I would like to pass in a P

7条回答
  •  时光说笑
    2021-01-06 14:19

    I'm not sure but maybe you need some like this:

    template< typename TypeT >
    struct ParamVector
    {
        template < typename NewTypeT >
        operator ParamVector< NewTypeT >()
        {
            ParamVector< NewTypeT > result;
            // do some converion things
            return result;
        }
    
        template< typename NewTypeT >
        ParamVector( const ParamVector< NewTypeT > &rhs )
        {
            // convert
        }
    
        template < typename NewTypeT >
        ParamVector& operator=( const ParamVector< NewTypeT > &rhs )
        {
            // do some conversion thigns
            return *this;
        }
    
    
    };
    ParamVector< double > d1;
    ParamVector< float > f1;
    f1 = d1;
    

    You can choose use conversion operator or operator= - I've provided both in my example.

提交回复
热议问题