When creating template classes in C++, if the type upon which the template will be specialized is intended to be a shared_ptr type, is it better to make T a shared_ptr type or make T a non-pointer type?
For example, which of the below classes is better, FirstExample or SecondExample? Or alternatively, is this entirely context-dependent? If so, can you an example of a context where FirstExample is better and a context where SecondExample is better?
In my case, clarity of usage is a higher priority than performance.
#include <iostream>
#include <memory>
using IntPtr = std::shared_ptr<int>;
template <class T>
class FirstExample
{
public:
FirstExample( std::shared_ptr<T> value )
:myData( value ) {}
std::shared_ptr<T> getData() const { return myData; }
private:
std::shared_ptr<T> myData;
};
template <class T>
class SecondExample
{
public:
SecondExample( const T& value )
:myData( value ) {}
T getData() const { return myData; }
private:
T myData;
};
int main(int argc, const char * argv[])
{
FirstExample<int> firstInstance( std::make_shared<int>( 10 ) );
IntPtr copyOfFirstInstanceData = firstInstance.getData();
SecondExample<IntPtr> secondInstance( std::make_shared<int>(10) );
IntPtr copyOfSecondInstanceData = secondInstance.getData();
return 0;
}