From: Vaclav Vesely (vaclav.vesely_at_[hidden])
Date: 2006-03-24 05:36:07


Hi,

In Best Practices section of shared_ptr is an example:

        void f(shared_ptr<int>, int);
        int g();

        void ok()
        {
            shared_ptr<int> p(new int(2));
            f(p, g());
        }

        void bad()
        {
            f(shared_ptr<int>(new int(2)), g());
        }

Best Practices refers to http://www.gotw.ca/gotw/056.htm (it deals with
auto_ptr, but for shared_ptr the conclusion is same). The article
proposes following solution:

        template<typename T, typename A1>
        shared_ptr<Type> new_shared_ptr(A1 a1)
        {
            return shared_ptr<Type>(new Type(a1));
        }

        void ok2()
        {
            f(new_shared_ptr<int>(2), g());
        }

However the conclusion of the article is, that ok1() is better than
ok2() because you have to write helper functions. I don't agree. IMHO
ok2 IS better and the helper functions should be in the library (so the
uses doesn't have to write them. What do you think?

Regards,
Vaclav