Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4018448
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:59:03+00:00 2026-05-20T09:59:03+00:00

As many other C++0x users I’m trying to make a smart pointer interface for

  • 0

As many other C++0x users I’m trying to make a smart pointer interface for my projects. Ideally, I’d like syntax like this, hiding both types and std::make_shared<T>() calls.

Foo::Ptr foo = Foo::shared();
Foo::UPtr unique_foo = Foo::unique();

I’d like to place declarations inside a struct to inherit from:

template <class T>
struct SmartDefs
{
   typedef std::shared_ptr<T> Ptr;
   typedef std::unique_ptr<T> UPtr;

   template <class... P>
   static Ptr shared(P&&... p)
   {
      return std::make_shared<T>(std::forward<P>(p)...);
   }

   template <class... P>
   static UPtr unique(P&&... p)
   {
      return std::unique_ptr<T>(new T(std::forward<P>(p)...));
   }
};

Now this works nicely by itself, but when inheritance comes into the picture I get problems, since the typedefs and shared/unique static methods are defined twice. I managed to work around this problem using some ugly macros, private inheritance and require more typing, but I’d like to avoid this:

#include <memory>
#include <iostream>

template <class T>
struct SmartDefs
{
   typedef std::shared_ptr<T> Ptr;
   typedef std::unique_ptr<T> UPtr;

   template <class... P>
   static Ptr shared(P&&... p)
   {
      return std::make_shared<T>(std::forward<P>(p)...);
   }

   template <class... P>
   static UPtr unique(P&&... p)
   {
      return std::unique_ptr<T>(new T(std::forward<P>(p)...));
   }
};

#define DECL_SMART(type) using SmartDefs< type >::Ptr; \
   using SmartDefs< type >::UPtr; \
   using SmartDefs< type >::shared; \
   using SmartDefs< type >::unique

class Foo : private SmartDefs<Foo>
{
   public:
      DECL_SMART(Foo);
      virtual void foo() const { std::cout << "Foo" << std::endl; }
};

class Bar : public Foo, private SmartDefs<Bar>
{
   public:
      DECL_SMART(Bar);
      void foo() const { std::cout << "Bar" << std::endl; }
};

template <class T>
struct Baz : private SmartDefs<Baz<T>>
{
   DECL_SMART(Baz<T>);
   void foo(const T& in) const { std::cout << in << std::endl; }
};

int main()
{
   auto foo = Foo::shared();
   auto bar = Bar::shared();
   auto baz = Baz<int>::shared();
   foo->foo();
   bar->foo();
   baz->foo(10);
   foo = bar;
   foo->foo();
}

I’ve tested something like this, but it still gives me ambiguous reference to shared():

template <class T>
struct SmartPtr : private SmartDefs<T>
{
   using SmartDefs<T>::Ptr;
   using SmartDefs<T>::UPtr;
   using SmartDefs<T>::shared;
   using SmartDefs<T>::unique;
};

class Foo : public SmartPtr<Foo> {};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:59:04+00:00Added an answer on May 20, 2026 at 9:59 am

    Could you expand on:

    Now this works nicely by itself, but when inheritance comes into the picture I get problems

    Do you mean multiple Inheritance of two base classes each inheriting from your SmartDefs class? Do you mean Inheriting from both the SmartDefs class and a base class which itself inherits from the SmartDefs class?

    Either way, your problem is not with C++0x of course, but general ambiguity of base members.

    Example:

    #include <iostream>
    
    struct A
    {
        typedef int ret_type;
        static ret_type go(){ std::cout << __PRETTY_FUNCTION__ << std::endl; }
    };
    struct B
    {
        typedef int ret_type;
        static ret_type go(){ std::cout << __PRETTY_FUNCTION__ << std::endl; }
    };
    struct C : public A, public B
    {
        typedef int ret_type;
        static ret_type go(){ std::cout << __PRETTY_FUNCTION__ << std::endl; }
    };
    
    int main()
    {
        C c;
    
        C::A::ret_type ago = c.A::go();
        C::B::ret_type bgo = c.B::go();
        C::ret_type    cgo = c.go();
    
        C::A::ret_type static_ago = C::A::go();
        C::B::ret_type static_bgo = C::B::go();
        C::ret_type    static_cgo = C::go();
    }
    

    So, you need to solve the problem the same way: explicitly disambiguate at call time, or, preferably, overload the functions/typedefs in your derived class.

    However, I am not sure I would recommend your solution at all. In the face of inheritance Derived::unique() would return a unique_ptr<Base> if Derived did not inherit from your SmartPtr class. The only “safe” way to implement something like this is with a virtual Create() function (in your case: CreateUnique, CreateShared perhaps).

    I would personally prefer to write a global make_shared to wrap std::make_shared and write my own make_unique in the same namespace.

    A::shared() // replace
    make_shared<A>() // with this
    
    A::unique() // replace
    make_unique<A>() // with this
    

    The few character difference here is trivial compared to not having to inherit every class from SmartDefs, and avoiding the big risk of improper use.

    Edit: I forgot to cover that you will lose typedefs for the return type this way. You could use type_traits, but I actually consider lack of typedef a feature here. Information aboyt core classes such as std::shared_ptr and std::unique_ptr need not be typedef-ed away, and between auto, templates, and decltype, there is little need for explicit typedefs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seen many other similar questions like this on this website and i would say
I have a 'legacy' DB2 database that has many other applications and users. Trying
Like many other users out here I have a problem with el and events.
Like many other SharePoint users, I've had to create a custom list definition. After
I have this custom JSlider, which will be used in many other forms/windows. But
we plan develop application that will, like many other modern applications is divided into
I have an InnoDB table containing users, like this: +--------------+-----------------------+ | user_id | name
I have this small app that loads plugin type components that other users can
ok I am well aware there are many other questions about this, but I
MongoDB Question: I would like to find similar bookmarks of other users. Let's say

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.