Reputation: 821
I am developing a C++ template library that uses both primitive types and pointer types stored in boost::shared_ptr. I am having a problem with a helper class that is used to pack primitive types into a container class if necessary before being passed into a lower layer of the library. The following shows the basic type that just passes the pointer on, and the implementation for std::string which is one of the primitives.
template <class T> class RRPrimUtil
{
public:
static rr_DataType GetTypeID() {return rr_void_t;}
static boost::shared_ptr<RRObject> PrePack(T val) {return rr_cast<RRObject>(val);}
static T PreUnpack(boost::shared_ptr<RRObject> val) {return rr_cast<T>(val);}
};
template<> class RRPrimUtil<std::string>
{
public:
static rr_DataType GetTypeID() {return rr_string_t;}
static boost::shared_ptr<RRObject> PrePack(std::string val) {return rr_cast<RRObject>(stringToRRArray(val));}
static std::string PreUnpack(boost::shared_ptr<RRObject> val) {return RRArrayToString(rr_cast<RRArray<char>>(val));}
};
The rr_cast<>() function is an alias to dynamic_pointer_cast. The problem I am having is that for the general case, the template "T" includes the "boost::shared_ptr" prefix because this type may or may not interact with a shared_ptr. The prefix messes up the dynamic_pointer_cast, because it expects just the pointer type. Is there a clean way to work around this?
Upvotes: 0
Views: 418
Reputation: 81349
A template metafunction will strip that for you:
template<typename T> struct primitive_type
{
typename T type;
};
template<typename T> struct primitive_type< boost::shared_ptr< T > >
{
typename T* type;
};
In case the type passed to it is in the form boost::shared_ptr< T >
it will return T*
; otherwise it will return the type it was instantiated with.
Upvotes: 4