tags:

views:

351

answers:

4

My company are currently not won over by the Boost libraries and while I've used them and have been getting them pushed through for some work, some projects due to their nature will not be allowed to use Boost. Basically libraries, like Boost, cannot be brought in for work so I am limited to the libraries available by default (Currently using Visual Studio 2005).

So... My question is, if I am unable to use Boost::shared_ptr and its little brothers, what is the alternative when using STL containers with pointers?

One option I see is writing a container class like a shared_ptr that looks after a given pointer, but I'd like to know if there are other alternatives first.

+3  A: 

If they're not going to accept boost, I presume other "not developed here" libraries are out of the question.

It seems to me you're left with two options:

  • Roll your own shared_ptr.
  • Use raw pointers, and manage the memory yourself.

Neither is ideal, and each comes with it's own pain. Your saving grace might be that you have all of the source to boost available to you. You can use it as a model for writing your own shared_ptr.

luke
A simple non-threadsafe shared_ptr shouldn't be a problem. Adding threadsafety may make the issue more complicated.
Matt_JD
There are good lit and good examples out there.
luke
I agree, depending on what you are doing, manual memory management (deleting all the pointers yourself before container destruction) may not be that difficult. I've also gone the same route and was forced to create our own shared_ptr implementation after we could not use boost in our codebase.
Brian Neal
@Matt_JD: Its a lot harder than you think to get a shared pointer correct (even for non-threadsafe code). Please look carefully at boost::shared_ptr code.
Martin York
It definitely isn't trivial to write your own correct shared_ptr, but it isn't impossible either, for an experienced C++ coder. Especially if you use the boost source code as a guide.
Brian Neal
Yup. Definitely use the boost source, so at least you are API compatible. And remind your management that you at any moment can replace your hand-rolled smart pointers with a field-tested, well-debugged, extensively optimized version, _for free_.
MSalters
I wrote my own version that suited the purposes of the problem. Turned out to not be so difficult. I didn't look at the boost code to avoid being accused of stealing ideas. It's not as complex as boost's but works perfectly for my problem. Thanks.
Matt_JD
+1  A: 

That definetly depends on what you want to do. It's not as if shared_ptr are absolutely necessary for a project that uses pointers.

If you really need them, import those classes/templates/functions you really need to your own project if possible without importing the whole boost lib.

StampedeXV
It's down to getting things company approved from a coding standards and a legal point of view (FOSS). Most people here are aware that the legal side is ok, but it's not been officially stated so importing any code from boost, or anywhere else, is not allowed. There are exceptions and I'm aiming to get Boost approved but currently it is not, unfortunately.
Matt_JD
You talked about writing the code yourself. My idea was that you take the boost code, but company internally you treat it as your own code. Advantage is, you know that many people already tested your code.
StampedeXV
+2  A: 

In Visual Studio 2008 there is available std::tr1::shared_ptr. I'm not sure it is available in VS2005, you should check.

Kirill V. Lyadvinsky
Nope, can't see it. I have been using TR1 on other work I've been doing but this work is using VS2005.
Matt_JD
Well, you still could copy its implementation. I believe that it more copyable then boost implementation.
Kirill V. Lyadvinsky
@JIa3ep: I seriously doubt this. It's the work of a commercial company (Dinkumware), after all.
sbi
@sbi, you are restricted to compile Microsoft version of STL on OpenSource platform (or using g++), but I can't see why you cannot compile it in VS2005. Microsoft licence don't restrict it (I can't find such restriction in licence).
Kirill V. Lyadvinsky
A: 

Without knowing the background, it's hard to say why boost's libraries aren't permitted. If the reason is to avoid complex dependencies, you can easily work around the issue: Almost all boost libraries work with only a simply #include header: in short, they don't need linking and thus avoid dll-hell or any variant thereof.

So, if external libraries aren't appreciated because of the complexities involved in linking (whether statically or dynamically) them, you can simply copy the boost headers you need into the project by hand and use them directly.

For clarity and to make future upgrades and maintenance easier I'd avoid renaming the boost libraries (so future coders know where the code came from). If "they" don't want such simple code inclusions, well, you could make the argument that quite a few boost headers are headed for inclusion in the spec, and that they'll save everyone a bunch of headaches and time. Legally, the boost license is specifically designed to be as easy and safe to integrate as possible: all files have an explicit license which permits all relevant things, and almost all libs have exactly the same license.

I'm curious though: why exactly aren't boost headers permitted?

Eamon Nerbonne
It's basically because some customer requirements state that external libraries are not allowed, some projects are safety critical and all code (including libraries) needs to be certified, some projects don't want FOSS for legal issues and overall that all code, libraries, applications, etc need to be approved for use and that means taking time getting approval, if at all. Some would get it, others would not and for those that would not I'm looking at alternatives.
Matt_JD
Simply copying the boost headers you need into your project by hand isn't so simple. Boost is so incestous you'll be including 100's of files before you know it. Yes they have a tool to break out libraries, and you better use it to avoid a frustrating experience.
Brian Neal
Note that it's not because you avoid link-time dependencies, you don't have them: exporting the 'same' included class from 2 separate dll's easily becomes a problem, as it even is, already, with standard containers.
xtofl
@Brian Very true. The Boost boosters (clever huh?) always say that "you just need the header file", omitting to say that you need a shedload of header files. Having said that, I'd advise anyone that needs a shared pointer to use the Boost version.
anon
Well, specifically for the pointer-wrapping classes anyhow, it is not the case that they depend on 100's of other boost headers. There are just a few, almost all of which you could probably refactor out in a morning.
Eamon Nerbonne