Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ templated typedef

I have a templated class

template <T>
class Example 
{
...
};

inside which there are many methods of the following type:

template <class U> <class V> method(....)

Inside these I use tr1::shared_ptr to U or V or T.

Its tedious typing tr1::shared_ptr<const U> or tr1::shared_ptr<const V>.

The obvious thing to do:

template <typename U>
typedef tr1::shared_ptr<U> shptr<U>;

does not work.

What do you do in this situation? Anything that can reduce verbosity?

like image 766
user231536 Avatar asked Feb 10 '10 17:02

user231536


1 Answers

You can use an inner type:

template <typename U>
struct sptr {
    typedef tr1::shared_ptr<U> t;
};

Then say sptr<U>::t, or unfortunately often typename sptr<U>::t.

C++0x has template typedefs, you could check whether your compiler can be persuaded to accept them:

template<typename U>
using sptr = tr1::shared_ptr<U>;

Then say sptr<U>

And of course there's always #define sptr ::tr1::shared_ptr, for example if you're expecting C++0x in future and want to bridge the gap. Or if you're using it in a narrow enough context that a macro isn't scary.

like image 52
Steve Jessop Avatar answered Oct 01 '22 09:10

Steve Jessop