template class with a reference parameter

V

Vladimir Jovic

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.
 
V

Vladimir Jovic

Vladimir said:
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 );
}

};
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top