Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simplify complicated template declarations

Tags:

c++

c++11

For example I would like to simplify the std::tr1::shared_pointer template class. I would like to have an alias for std::tr1::shared_pointer.

But this doesn't work:

#include <tr1/memory>

template <class T>
class SharedPointer : public std::tr1::shared_ptr<T>
{
};

int main(int argc, char *argv[])
{
    SharedPointer<int> test(new int(5));
    return 0;
}

Since the constructors are not inherited.

Is there a pattern to solve this?

like image 864
Martin Drozdik Avatar asked Dec 16 '22 23:12

Martin Drozdik


1 Answers

If you want to alias it, a using declaration will create a true alias, rather than a subclass:

template<class T>
using SharedPointer = std::tr1::shared_ptr<T>;

Edit

A method that should work on GCC 4.6.* compilers would be to make a wrapper function around the std::tr1::shared_ptr<T>. Since GCC 4.6.* supports the C++11 library, I've used that instead of TR1:

#include <memory>
#include <utility>

template<typename T, typename... Args>
std::shared_ptr<T> make_shared_ptr(Args &&... args)
{
    return std::shared_ptr<T>(std::forward<Args>(args)...);
}

int main(int argc, char *argv[])
{
    auto test = make_shared_ptr<int>(new int(5));
    return 0;
}
like image 159
Ken Wayne VanderLinde Avatar answered Jan 01 '23 17:01

Ken Wayne VanderLinde