Migrate away from std::unary _function with tricky signiture

  Kiến thức lập trình

I need to migrate an ancient codebase to support c++17. This means removing auto_ptr, unary_function etc. I’ve already removed several instances of unary_function successfully but this one is a puzzler.

Here are the prerequisites: Traits macro, RemoveConstRef template.

template <class T>
struct RemoveConstRef {
    typedef T type;
};

#define FPTRAITS_BASIC( _type, _paramType )                                                                            
    template <>                                                                                                        
    struct Traits<_type> {                                                                                             
        typedef _type param_type;                                                                                      
        typedef _type return_type;                                                                                     
                                                                                                                       
        static int fp_param_type() { return _paramType; }                                                              
        static int fp_return_type() { return _paramType; }                                                             
                                                                                                                       
        static param_type get_parameter( FPValue& fpValue ) { return FP_FIELD( _paramType, fpValue ); }                
        static void get_return_value( FPValue& fpOutValue, return_type val ) {                                         
            return fpOutValue.LoadPtr( _paramType, FP_RSLT( _paramType, val ) );                                       
        }                                                                                                              
    };

FPTRAITS_BASIC( int, TYPE_INT )
FPTRAITS_BASIC( float, TYPE_FLOAT )

...

Original implimenation


    template <class T, int Index>
    struct get_parameter : public std::unary_function< FPParams*, typename Traits< typename RemoveConstRef<T>::type >::param_type >{
        inline result_type operator()( FPParams* p ) const {
            return Traits< typename RemoveConstRef<T>::type >::get_parameter( p->params[Index] );
        }
    };

c++17 attempt

    template <class T, int Index>
    struct get_parameter : public std::function<typename Traits< typename RemoveConstRef<T>::type >::param_type(FPParams*) >{
        inline result_type operator()( FPParams* p ) const {
            return Traits< typename RemoveConstRef<T>::type >::get_parameter( p->params[Index] );
        }
    };

These are the compiler errors:

error C2143: syntax error: missing ‘;’ before ‘(‘ at the operator keyword

2

I think you’re missing the point of std::unary_function versus std::function. The two are entirely unrelated, and it makes no sense to rewrite get_parameter like you did.

std::unary_function provided argument_type and result_type. With new C++11 features like auto, those typedefs are no longer necessary. Just remove it entirely. The return type of operator() can simply become auto.

3

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT