Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > template class with a reference parameter

Reply
Thread Tools

template class with a reference parameter

 
 
Vladimir Jovic
Guest
Posts: n/a
 
      12-10-2010
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.
 
Reply With Quote
 
 
 
 
Vladimir Jovic
Guest
Posts: n/a
 
      12-10-2010
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 );
}

};
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Declaring a template class with two template params a friend in anon-template class A L C++ 1 08-25-2010 07:25 AM
decltype as a template parameter when containing reference to anothertemplate parameter. Isti C++ 2 04-19-2010 10:01 PM
Using declaration inside first template parameter as default valuefor second template parameter. Stuart Redmann C++ 5 12-14-2007 08:42 AM
template class instantiate without template parameter, automatictype deduction Fei Liu C++ 0 10-25-2007 08:12 PM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM



Advertisments