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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:22:48+00:00 2026-06-03T13:22:48+00:00

Introduction Peter Weinhart describes how to design a generic intrusive_ptr base class using CRTP

  • 0

Introduction

Peter Weinhart describes how to design a generic intrusive_ptr base class using CRTP, which may be used as follows:

class foo : intrusive_base<foo>
{
     // foo-specific code.
};

This approach imposes the constraint that all foo objects carry a reference counter. Assume that we keep foo sometimes by value and only want to pay the price of the reference counter when we have a pointer. For example, sometimes we would like to create foo instances and just move them around, and sometimes we want to allocate foo on the heap.

Conceptually, the right mechanism for this scenario is a std::shared_ptr. However, there are certain scenarios requiring raw pointers that would call for an intrusive pointer, e.g., when passing pointers through a C API that take void pointers. In this case, one would “ref” the pointer before passing it to the opaque API and “unref” when getting it back.

Having control over foo, probably the best method would be to use a policy-based implementation and have a reference-counted and basic version of foo. Without having control over foo, an alternative design would to invert the inheritance relationship:

template <typename Base>
class intrusive : public Base
{
    // ?

private:
    std::atomic_size_t ref_count_;   
};

typedef intrusive<foo> intrusive_foo;

// Assume boost::intrusive_ptr as intrusive pointer implementation
boost::intrusive_ptr<intrusive_foo> x = new intrusive_foo;
{
    auto y = x;   // Semantics: boost::intrusive_ptr_add_ref(x.get())

    // At scope exit: boost::intrusive_ptr_release(x.get())
}

In the above mentioned article, Peter says that a such a “generic implementation of [intrusive] would make use of C++0x variadic templates and perfect forwarding.”

Question

How would the implementation of such a generic intrusive class look like? I could see that it may benefit from C++11 inheriting constructors, but it is unclear to me how one would in fact implement the body of intrusive using the mentioned tools.

  • 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-03T13:22:50+00:00Added an answer on June 3, 2026 at 1:22 pm

    Using make_shared gives you the same efficiency as an intrusive pointer.

    In this case, one would “ref” the pointer before passing it to the opaque API and “unref” when getting it back.

    As someone else said, you can use enable_shared_from_this to get a shared_ptr back from a raw pointer (as long as there is at least one shared_ptr somewhere in the system which still owns the object)

    But to answer the main question, I assume he means using variadic templates and perfect forwarding to define a constructor, which would look like:

    template <typename Base>
      class intrusive : public Base
      {
        template<typename... Args>
          intrusive(Args&&... args)
          : Base(std::forward<Args>(args)...), ref_count_(0)
          { }
    

    This allow you to construct the intrusive with any number of arguments of any type and they will be forwarded to the Base, so you can construct it with any arguments that could be used to construct a Base.

    Another alternative would be to use C++11 inheriting constructors (which aren’t implemented in any compiler AFAIK)

    template <typename Base>
      class intrusive : public Base
      {
        using Base::Base;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Introduction We have an OpenID Provider which we created using the DotNetOpenAuth component. Everything
Introduction I have been so annoyed by applications that have a startup dialog which
INTRODUCTION I'm using excel downloads as a way of users downloading a score sheet,
Introduction In a current project I'm working on we're using the ChartBoost SDK for
Introduction I am attempting to replace an on-call cell phone which is carried by
Introduction I'm currently doing some bug fixes in an application which is in the
Introduction: I have a flat ArrayCollection of object's, which i group to create the
Introduction: I think Nape is a relatively new engine so some of you may
Just doing a little introduction to JavaScript. I'm used to more than often testing
In A Brief Introduction to Rvalue References , forward is defined as follows: template

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.