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

  • Home
  • SEARCH
  • 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 6714657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:31:16+00:00 2026-05-26T08:31:16+00:00

I’m creating a c++ library that makes heavy use of smart pointers, both in

  • 0

I’m creating a c++ library that makes heavy use of smart pointers, both in public interface and internally. Currently it is “header-only”, but as it evolves it is becoming clear that this may not be appropriate.

I don’t want to dictate a choice of smart pointer implementation to library users. I also have gotten pretty far without bringing in any dependencies (such as boost), and would like to stay dependency-free as long as possible. The library isn’t targeting c++0x, and (unsurprisingly) wants to stay as standards-compliant as possible.

Currently, my solution for letting users select an implementation looks like this:

#ifndef MYLIB_MEMORY_H_
#define MYLIB_MEMORY_H_

#if MYLIB_STD_SMART_PTR
  #include <memory>
  #define MYLIB_SHARED_PTR std::shared_ptr
  #define MYLIB_WEAK_PTR std::weak_ptr
  #define MYLIB_STATIC_POINTER_CAST std::static_pointer_cast
#elif MYLIB_STD_TR1_SMART_PTR
  #include <tr1/memory>
  #define MYLIB_SHARED_PTR std::tr1::shared_ptr
  #define MYLIB_WEAK_PTR std::tr1::weak_ptr
  #define MYLIB_STATIC_POINTER_CAST std::tr1::static_pointer_cast
#elif MYLIB_BOOST_SMART_PTR
  #include <boost/shared_ptr.hpp>
  #define MYLIB_SHARED_PTR boost::shared_ptr
  #define MYLIB_WEAK_PTR boost::weak_ptr
  #define MYLIB_STATIC_POINTER_CAST boost::static_pointer_cast
#else
  #error Please define one of MYLIB_STD_SMART_PTR, MYLIB_STD_TR1_SMART_PTR or MYLIB_BOOST_SMART_PTR
#endif

#endif // MYLIB_MEMORY_H_

Since the library is currently header-only, choosing a macro at (client) compile time is sufficient. I’m afraid that if I move to a shared library, this trick will break down and I’ll have to find another way.

What might that other way be?

  • 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-26T08:31:16+00:00Added an answer on May 26, 2026 at 8:31 am

    Not an answer

    I would take a different approach to the selection of the shared pointer, mainly because I dislike macros.

    #if MYLIB_STD_SMART_PTR
      #include <memory>
    namespace mylib {
      using std::shared_ptr;
      using std::weak_ptr;
      using std::static_pointer_cast;
    }
    #elif MYLIB_STD_TR1_SMART_PTR
      #include <tr1/memory>
    namespace mylib {
      using std::tr1::shared_ptr;
      using std::tr1::weak_ptr;
      using std::tr1::static_pointer_cast
    }
    #elif MYLIB_BOOST_SMART_PTR
      #include <boost/shared_ptr.hpp>
    namespace mylib {
      using boost::shared_ptr;
      using boost::weak_ptr;
      using boost::static_pointer_cast;
    }
    #else
      #error Please define one of MYLIB_STD_SMART_PTR, MYLIB_STD_TR1_SMART_PTR or MYLIB_BOOST_SMART_PTR
    #endif
    

    Now, relating to the actual question, if the smart pointers are used as part of the public interface of a compiled in library you will have to provide different binaries and have the user pick which binary to link against. Then again, I would try avoiding the use of shared_ptr in the API of a library, as it forces your choice of smart pointer onto your users (unless, of course, your library does share the pointers with the clients), and rather use std::auto_ptr that has the advantage of being able to manage the memory automatically and at the same time allow the user to decide to move the resource to any other type of pointer by calling release() (once a shared_ptr obtains ownership of a resource, it cannot give ownership back).

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

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I need to clean up various Word 'smart' characters in user input, including but
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim

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.