template class with a reference parameter

Discussion in 'C++' started by Vladimir Jovic, Dec 10, 2010.

  1. Hello,

    I am having a template classes like this :

    template< typename R, typename A1 >
    class FuncTypeGeneral
    {
    typedef R return_type;
    typedef A1 arg1_type;
    };

    template < typename FuncType >
    class PublisherAdapterHelper
    {
    public:

    typedef typename FuncType::arg1_type arg1_type;


    void ExecuteTheSlot( void **arguments )
    {
    const arg1_type &arg1 = GetArg1< arg1_type >( arguments[1]
    }

    private :

    template< typename T >
    arg1_type & GetArg1( void * arg ) const
    {
    return * reinterpret_cast< arg1_type* > ( arg );
    }
    arg1_type GetArg1( void * arg ) const
    {
    return * reinterpret_cast< arg1_type* > ( arg );
    }

    };


    Now if I instantiate this template with a non-reference parameter, it
    compiles fine. For example like this :
    template class PublisherAdapterHelper< FuncTypeGeneral< void, int > >;

    But as soon as I instantiate it with a reference, it breaks the
    compilation, because it tries to convert the cast to arg1_type&* :
    template class PublisherAdapterHelper< FuncTypeGeneral< void, int & > >;

    So, why the templated method is better fit in this case? It shouldn't be
    called, right? The normal function looks like a better fit.
    Vladimir Jovic, Dec 10, 2010
    #1
    1. Advertising

  2. Vladimir Jovic wrote:
    > Hello,
    >
    > I am having a template classes like this :
    >
    > template< typename R, typename A1 >
    > class FuncTypeGeneral
    > {
    > typedef R return_type;
    > typedef A1 arg1_type;
    > };
    >
    > template < typename FuncType >
    > class PublisherAdapterHelper
    > {
    > public:
    >
    > typedef typename FuncType::arg1_type arg1_type;
    >
    >
    > void ExecuteTheSlot( void **arguments )
    > {
    > const arg1_type &arg1 = GetArg1< arg1_type >( arguments[1]
    > }
    >
    > private :
    >
    > template< typename T >
    > arg1_type & GetArg1( void * arg ) const
    > {
    > return * reinterpret_cast< arg1_type* > ( arg );
    > }
    > arg1_type GetArg1( void * arg ) const
    > {
    > return * reinterpret_cast< arg1_type* > ( arg );
    > }
    >
    > };
    >
    >
    > Now if I instantiate this template with a non-reference parameter, it
    > compiles fine. For example like this :
    > template class PublisherAdapterHelper< FuncTypeGeneral< void, int > >;
    >
    > But as soon as I instantiate it with a reference, it breaks the
    > compilation, because it tries to convert the cast to arg1_type&* :
    > template class PublisherAdapterHelper< FuncTypeGeneral< void, int & > >;
    >
    > So, why the templated method is better fit in this case? It shouldn't be
    > called, right? The normal function looks like a better fit.



    I managed to solve the issue by adding a type conversion template :

    template < typename T >
    struct TypeConv
    {
    typedef T value_type;
    };

    template < typename T >
    struct TypeConv< T & >
    {
    typedef T value_type;
    };

    and changing the template to this :

    template < typename FuncType >
    class PublisherAdapterHelper
    {
    public:

    typedef typename FuncType::arg1_type arg1_type;
    typedef typename TypeConv< arg1_type > arg1_value_type;


    void ExecuteTheSlot( void **arguments )
    {
    const arg1_value_type &arg1 = GetArg1( arguments[1]
    }

    private :

    arg1_value_type GetArg1( void * arg ) const
    {
    return * reinterpret_cast< arg1_value_type* > ( arg );
    }

    };
    Vladimir Jovic, Dec 10, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page