开发者

typedef to template type

What's wrong with the following?

typedef boost::shared_ptr SharedPtr;

GCC gives the following error:

ISO C++ forbids declaration of ‘shared_ptr’开发者_如何学Go with no type


C++ doesn't (yet) have "template typedefs" where you can "rename" a template like this. This is a feature being added in C++0x, where such a "typedef" is called an "alias template."

The simplest workaround that works today is to use a class template with a nested typedef:

template <typename T>
struct SharedPtr
{
    typedef std::shared_ptr<T> Type;
};

// usage
typename SharedPtr<int>::Type sp;


C++11:

template<typename T>
using SharedPtr = boost::shared_ptr<T>:

Or you could use shared_ptr built into C++11:

template<typename T>
using SP = std::shared_ptr<T>;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜