patterncppMinor
make_auto implementation for C++03 compilers
Viewed 0 times
make_autoimplementationforcompilers
Problem
I want to provide, in the the spirit of C++11/boost make_shared and C++14 make_unique, a production-ready make_auto for C++03 compilers.
So, inspired boost's make_shared implementation for C++03 compilers, I provide N overloads (0 to 10, currently) of the same make_auto function.
Is there something wrong with this code?
Implementation details:
Background info:
I need this for multiple reasons, among them:
So, inspired boost's make_shared implementation for C++03 compilers, I provide N overloads (0 to 10, currently) of the same make_auto function.
template
inline std::auto_ptr make_auto()
{
return std::auto_ptr(new T()) ;
}
template
inline std::auto_ptr make_auto(const T_0 & p_0)
{
return std::auto_ptr(new T(p_0)) ;
}
template
inline std::auto_ptr make_auto(const T_0 & p_0, const T_1 & p_1)
{
return std::auto_ptr(new T(p_0, p_1)) ;
}
// etc.Is there something wrong with this code?
Implementation details:
- the const reference is inspired by boost's shared_ptr. After trying multiple alternatives (including GManNickG's excellent answer here), I had to gave up because of some challenged compilers. To pass non-const references, I'll use boost::ref.
- the
inlinekeyword is mandatory for reasons that have nothing to do with the problem.
Background info:
I need this for multiple reasons, among them:
- I am stuck with C++03 compilers (and I suspect some of them of not even being that... I'm looking at you, SunStudio). This means even C++03 emulations of unique_ptr don't work (we tried)
- avoiding writing new/delete in my C++ code (see Stroustrup's C++11 style video)
- offering my coworkers the means to use a more modern C++ coding style despite aforementioned compilers limitations (RAII, that is)
- more exception safety
- I need a lightweight smart pointer with unique-ownership semantics, able to transfer ownership, so boost::shared_ptr and boost::scoped_ptr are not an option
Solution
Disclaimer: this is not an attempt to answer your technical question, but suggesting alternatives to reach some compromise of your stated goals. It wouldn't fit in a comment :-)
I am not sure it is a good idea to actually write a
You'd be better off with emulating a
I'd take that as the preferred option, with using
Please also make sure to introduce your team to the other language emulation features that Boost provides (Foreach, Move).
I am not sure it is a good idea to actually write a
make_auto. It is akin to rearranging the deck chairs on the Titanic. After it hit the iceberg.You'd be better off with emulating a
std::unique_ptr using C++98/03 features. Howard Hinnant has one version on his website. You might even get lucky and get its macro variadic version to work nicely with the std::make_unique proposal by Stephan T. Lavavej.I'd take that as the preferred option, with using
boost::shared_ptr or boost_scoped_ptr as the preferred alternative option. If you insist on keeping std::auto_ptr, make its use as painful as possible. Without pain, you'll find it more difficult to convince your colleagues to upgrade to C++11.Please also make sure to introduce your team to the other language emulation features that Boost provides (Foreach, Move).
Context
StackExchange Code Review Q#26742, answer score: 4
Revisions (0)
No revisions yet.