Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should a library use an interface that uses smart pointers?

I'm starting to write a library and considering its interface. Previous libraries I've written all use raw pointers (both internally and in its interface), and now I want to try the smart pointer library that comes with VS2010.

  1. Should the interface use smart pointers? (Possibly forcing the library users to use smart pointers too?)
  2. Would it be messy if the interface uses raw pointers but the library uses smart pointers internally? (Is it even possible? shared_ptr doesn't have a release() method...)
  3. Can two c++0x compliant smart pointer libraries (say boost and VS2010) be used interchangeably? (say I use VS2010 to write my library and the users use boost)

Please help :)

like image 682
twf Avatar asked Aug 28 '10 04:08

twf


People also ask

Which library must be included to use smart pointers?

In modern C++ programming, the Standard Library includes smart pointers, which are used to help ensure that programs are free of memory and resource leaks and are exception-safe.

Should I always use a smart pointer?

So, a smart pointer is only needed, when you use new or other means of dynamic memory allocation. In my opinion, you should prefer to allocate variables on the stack, so when refactoring code (to C++11), you should always ask yourself, if this new is needed, or could be replaced with an object on the stack.

Why should you use smart pointers instead of regular pointers?

Smart pointers try to prevent memory leaks by making the resource deallocation automatic: when the pointer to an object (or the last in a series of pointers) is destroyed, for example because it goes out of scope, the pointed object is destroyed too.

What is a smart pointer and why it is needed?

A Smart Pointer is a wrapper class over a pointer with an operator like * and -> overloaded. The objects of the smart pointer class look like normal pointers. But, unlike Normal Pointers it can deallocate and free destroyed object memory.


1 Answers

It is imposable to answer those question without understanding a lot more about your design principles and how you expect the library to be used.

So I can only answer based on my experience and how I like my libraries to be used.

  1. Yes.
  2. Yes. Don't do it.
  3. Its probably not a good idea to mix them (though I have never tried).
    But you can compensate for this:
    As most open source is distributed as source you can build your source so that it can be configured for use in many environments.

For Example:

#if   defined(MY_PROJ_SHARED_PTR_FROM_BOOST)

#include <boost/shared_ptr.hpp>
#define MY_PROJ_SHARED_PTR_NAMESPACE    boost

#elif defined(MY_PROJ_SHARED_PTR_FROM_STD)

#include <memory>
#define MY_PROJ_SHARED_PTR_NAMESPACE    std

#elif defined(MY_PROJ_SHARED_PTR_FROM_TR1)

#include <tr1/memory>
#define MY_PROJ_SHARED_PTR_NAMESPACE    std::tr1

#else
#error "MY_PROJ_SHARED_PTR_FROM_<XXX> not defined correctly"
#endif


namespace X
{
    using ::MY_PROJ_SHARED_PTR_NAMESPACE::shared_ptr;
}


int main()
{
    X::shared_ptr<int>  data;
}

I am sure there are other ways to do this.
But it is late.

like image 75
Martin York Avatar answered Sep 25 '22 07:09

Martin York