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 8005271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:10:57+00:00 2026-06-04T17:10:57+00:00

I’m trying to write a smart pointer wrapper (contains a boost shared_ptr or scoped_ptr

  • 0

I’m trying to write a smart pointer wrapper (contains a boost shared_ptr or scoped_ptr or another smart pointer wrapper); each wrapper type injects a bit of extra functionality (eg. logging usage, lazy init, verifying correct construction/destruction order, etc) but I want them to be as invisible as possible to the user (such that I could swap in/out wrappers simply by changing a single typedef, and possibly some constructor code, but none of the usage code).

Normal usage is trivial:

template<typename T>
class some_smart_ptr_wrapper
{
public:
    typedef typename T::value_type value_type;
    ...
    value_type* get() { /*do something magic*/ return m_ptr.get(); }
    const value_type* get() const { /*do something magic*/ return m_ptr.get(); }
    // repeat for operator->, operator*, and other desired methods
private:
    T m_ptr;
};
typedef some_smart_ptr_wrapper< boost::shared_ptr<int> > smart_int;
smart_int p;

(Now all other code can use p indistinguishably from a shared_ptr, at least for the defined operations, and I can add extra wrappers just by changing the typedef.)

Multiple wrappers can be used simultaneously simply by nesting them as you’d expect, and only the code that declares (and sometimes initialises) the variable needs to care.

Occasionally though it’s useful to be able to get the “basic” shared/scoped_ptr from a wrapper (particularly to pass a shared_ptr as an argument to a function that doesn’t need to trigger the functionality added by the wrappers); for a single wrapper layer it’s easy:

    T& getPtr() { return m_ptr; }
    const T& getPtr() const { return m_ptr; }

But this doesn’t scale nicely to multiple wrapper layers (the caller has to do p.getPtr().getPtr().getPtr() the correct number of times).

What I’d like to be able to do is to declare getPtr() such that:

  • if T implements getPtr(), then it returns m_ptr.getPtr() (whatever type that is).
  • if T does not implement getPtr(), then it returns m_ptr.
  • it’s still available in both const and non-const flavours, as you’d expect.

The net result of which is that a single call to getPtr() from the outside code will “walk up” the chain to the original smart pointer, since that will be the only one that doesn’t implement getPtr().

I’m fairly sure that the solution will involve SFINAE and boost::enable_if; I’ve had a bit of a play towards trying to get something like that to work but haven’t had much luck thus far.

Solutions or completely alternate approaches both welcomed; note that I would like it to work in both VC++2008 and GCC (ie. no C++11, sadly).

  • 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-06-04T17:10:59+00:00Added an answer on June 4, 2026 at 5:10 pm

    (1) Declare a special member typename which is unique inside:

    template<typename T>
    class some_smart_ptr_wrapper
    {
    public:
      typedef T smart_type;  // <-- unique name
    ...
    };
    

    (2) Wrap getPtr() inside a template function wrapper:

    template<typename T>
    class some_smart_ptr_wrapper
    {
    ...
    public:
      T& getPtr () { return m_ptr; }
    
      typename My_sfinae<T>::RealType& getFinalPtr ()
      { return My_sfinae<T>::getPtr(m_ptr); }
    ...
    };
    

    (3) Where My_sfinae is implemented as usual:

    template<typename T>
    struct void_ { typedef void type; };
    
    template<typename T, typename = void>
    struct My_sfinae {
      typedef T RealType;
    
      static T& getPtr (T &p) { return p; }
    };                        //^^^^^^^^^ business logic!
    template<typename T>
    struct My_sfinae<T, typename void_<typename T::smart_type>::type> {
      typedef typename My_sfinae<typename T::smart_type>::RealType RealType;
    
      static RealType& getPtr (T &p)
      { return My_sfinae<typename T::smart_type>::getPtr(p.getPtr()); }
    };
    

    As you can see, My_sfinae removes all your wrappers recursively and finally you are left with the final m_ptr.

    Here is the demo.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

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.