• Advertisement
Sign in to follow this  

typename operators

This topic is 4765 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Time for me to make another post Does c++ support any kind of typename operations? Basically I have a templated function & I pass it a pointer type. I then need to derive a reference type from that so that I can pass it onto the new operator. or using typedefs.. The reverse:
typedef char character
typedef *character text
What I'm doing:
typedef *char text
typedef &text character
(Why doesn't c++ support this?) I realise I could add an extra template parameter for the function to specify the reference type but what I wish to know is: Is there a way of deriving a reference type from a template pointer type? I fear a straight "no" is coming but I must @ least ask

Share this post


Link to post
Share on other sites
Advertisement
You have a few options. For one, you can just make your function like this:


template< typename ValueType >
void your_function( ValueType* your_pointer )
{
typedef ValueType& reference_type;
}



or you can do:


template< typename Type >
struct pointer_to_reference;

template< typename ValueType >
struct pointer_to_reference< ValueType* >
{
typedef ValueType& type;
};

template< typename PointerType >
void your_function( PointerType some_pointer )
{
typedef typename pointer_to_reference< PointerType >::type reference_type;
// reference_type is now the same as a reference to
// an instance of the type that the pointer points to.

}



If you have boost installed, you can also use boost::remove_pointer and boost::add_reference appropriately (in the type_traits sublibrary).

Share this post


Link to post
Share on other sites
Cheers. *cough* put the operator on the wrong side of the type. damn i always do that. But thanks, I had a good firm look through boost (bigger than I thought) - lots of good stuff to learn there. Well this was an area I was shaky on, but thanks to the direction you gave I'm comfortable with it now. Thanks for taking the time

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement