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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:00:38+00:00 2026-05-20T19:00:38+00:00

Given class Foo template <typename T> class Foo { public: …other methods.. void bar()

  • 0

Given class Foo

template <typename T>
class Foo
{
public:

  ...other methods..

  void bar()
  {
    ...
    m_impl.doSomething();
    ...
  }

  void fun()
  {
    ...
    m_impl.doSomethingElse();
    ...
  }

  void fubar()
  {
    ...
  }

private:
  T m_impl;
};

I wanted to cater for situations where T is a boost::shared_ptr.
In this case the only change to class Foo is that it should invoke

m_impl->doSomething();

instead of

m_impl.doSomething();

I ended up defining FooPtr in the same header

template <typename T>
class FooPtr
{
public:
  ...other methods..

  void bar()
  {
    ...
    m_pImpl->doSomething();
    ...
  }

  void fun()
  {
    ...
    m_pImpl->doSomethingElse();
    ...
  }

  void fubar()
  {
    ...
  }

private:
  boost::shared_ptr<T> m_pImpl;
};

Now while the approach works for all classes that I want to use with Foo,
the problem is that I have a lot of duplicate code lying around and any changes
I make to Foo, I also have to make to FooPtr.

How can I refactor the code? E.g. Is there any way that I can determine at compile time if T is of type boost::shared_ptr, and then specialise just the bar and fun methods to invoke the -> operator?

Edit:
Thanks for all the answers so far! I just need some time to work through them all and see which solution is the best fit for our software.

Edit 2:
@Matthieu: This is the test code I was using

class FooImpl
{
public:
  void doIt()
  {
    cout << "A" << std::endl;
  }
};

int _tmain(int argc, _TCHAR* argv[])
{
  Foo<FooImpl> foo;
  foo.doSomething();
  return 0;
}
  • 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-20T19:00:39+00:00Added an answer on May 20, 2026 at 7:00 pm

    Sylvain wrote a DRY solution, but I don’t like abusing inheritance.

    Using a wrapper class to uniformize the interface is easy, especially since pointer semantics work so well!

    namespace details {
      template <typename T>
      struct FooDeducer {
        typedef boost::optional<T> type;
      };
    
      template <typename T>
      struct FooDeducer< T* > {
        typedef T* type;
      };
    
      template <typename T>
      struct FooDeducer< boost::shared_ptr<T> > {
        typedef boost::shared_ptr<T> type;
      };
    } // namespace details
    
    template <typename T>
    class Foo {
    public:
      // methods
      void doSomething() { impl->doIt(); }
    
    private:
      typedef typename details::FooDeducer<T>::type Type;
      Type impl;
    };
    

    Here, relying on boost::optional which provides the OptionalPointee semantics, we nearly get the same behavior than pointers.

    One point I’d like to emphasize though, is the difference in the copying behavior. boost::optional provides deep copy.

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

Sidebar

Related Questions

Given a declaration like this: class A { public: void Foo() const; }; What
So given the following template functions with partial specialization template<typename T> void foo(vector<T> &in)
Given the following template: template <typename T> class wrapper : public T {}; What
Given a C++ template class (or function) definition: template<typename T> struct Foo { T
Given a class like this: class Foo { public: Foo(int); Foo(const Foo&); Foo& operator=(int);
Given the following class public class Foo { public int FooId { get; set;
Given: namespace A { class Foo; class Bar; } namespace B { class Foo;
Given this class class Foo { // Want to find _bar with reflection [SomeAttribute]
Given a variable foo of type FooClass* and a member variable in that class
I want to specialize a class template with the following function: template <typename T>

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.