Jump to content
  • Advertisement
Sign in to follow this  
Soaps79

shared_ptr in VS 2008

This topic is 2607 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Sorry for the lame question, but I can't find a definitive answer anywhere.

I wrote a system that I really like using C++ in VS 2010. I #include'd <memory>, and had access to std::shared_ptr. I am now trying to bring the system into a team project I am working on in VS 2008. I read that shared_ptr and other classes/libraries were not originally in VS2008, but were added in SP1. My VS About window says:

Version 9.0.21022.8 RTM
Microsoft .NET Framework Version 3.5 SP1

Shouldn't I have access to shared_ptr's? I tried:
[source lang="cpp"]
#include <memory>
using std::shared_ptr;
[/source]
and
[source lang="cpp"]
#include <memory>
using std::tr1::shared_ptr;
[/source]
Both told me that shared_ptr is not a member of the namespace. Any help would be much appreciated, thank you.

Share this post


Link to post
Share on other sites
Advertisement
std::shared_ptr is a part of C++ 11, something that didn't exist when VS2008 was released, and something that wasn't finalized when VS2010 was released (hence VS2010 supports part of C++11, like std::shared_ptr, but not all of the features).

If you want to use it, I would suggest using Boost's shared_ptr, which the C++11 std::shared_ptr was based off of, so it will have the behavior you're looking for, though Boost's shared_ptr exists in the boost namespace (and you'll have to install the boost headers so you can include them).

Like this:

#include <iostream>
#include <boost/shared_ptr.hpp>

int main()
{
boost::shared_ptr<int> x;
std::cout << x << std::endl;
x.reset(new int);
std::cout << x << std::endl;
*x = 100;
std::cout << *x << std::endl;
}

Share this post


Link to post
Share on other sites
I thought the same thing, about it being AFTER VS2008, but then I read that it was included in a Feature Pack and a few sources say it should be included in SP1. I just wasn't sure if there was a header I needed to include in the IDE settings that it may not have added automatically.

My problem is, this is a school project, and worked on on school desktops often. This kills any thoughts of Boost, since we don't have admin privileges to install anything. Could I download the library and just include the appropriate headers locally, or would this cause problems?

Also, I have a teammate who is always trying to stay "pure", and after the barking about smart pointers. trying to talk him into using an outside library may be more trouble than it's worth. I'm just bummed because I love the system I wrote, but it is very dependent on smart pointers, and it would require a lot of (hackish) rewriting if it had to manage its own memory.

Thanks for the input guys, I'll have to look into using Boost classes without installing it.

Share this post


Link to post
Share on other sites

I thought the same thing, about it being AFTER VS2008, but then I read that it was included in a Feature Pack and a few sources say it should be included in SP1. I just wasn't sure if there was a header I needed to include in the IDE settings that it may not have added automatically.

Yes, that's correct, shared_ptr should be included in VS2008 SP1, although I probably should have mentioned that your VS2008 instance does not have SP1 installed. I have the same version of VS2008 installed on my machine as you do. We would both need to install SP1 in order to use shared_ptr. However, it sounds like that isn't an option for you, unfortunately.


My problem is, this is a school project, and worked on on school desktops often. This kills any thoughts of Boost, since we don't have admin privileges to install anything. Could I download the library and just include the appropriate headers locally, or would this cause problems?

Yeah, boost::shared_ptr is a header only library (i.e. you don't have to build it and link to it). So if you just download the latest version of Boost, extract it, and put the header files in your Visual Studio solution's directory, you should be good to go. Depending on exactly what folder you put the Boost headers into, you may have to add the path to Visual Studio's Include Path (under Project->Properties->C/C++->General->Additional Include Directories). You don't actually have to "install" anything (though if you were going to use a non-header only library, like Boost's thread library, I'd suggest using Boost Pro to install the prebuilt binaries, which doesn't really "install" them as much as it does copy and paste them into a specified directory).


Also, I have a teammate who is always trying to stay "pure", and after the barking about smart pointers. trying to talk him into using an outside library may be more trouble than it's worth. I'm just bummed because I love the system I wrote, but it is very dependent on smart pointers, and it would require a lot of (hackish) rewriting if it had to manage its own memory.

Let me talk to him. Boost is used by professionals in the industry. It's tried and tested. Heck, many people consider Boost to practically be a fundamental part of C++, and in a way it kind of is, because a lot of things in the new C++ standard came from Boost. Smart pointers are for smart programmers. Period. The only time I would advise against using smart pointers is if there was some kind of actual limitation in the project preventing the use of smart pointers, but I've never run into that problem. And I would always advise someone to use a pre-made smart pointer library (like Boost's, or C++11's), unless the person has a real desire to implement one purely for education purposes, or if the programmer is 200% sure of what he is doing and has some kind of fundamental issue that an already existing library can't solve.

Share this post


Link to post
Share on other sites
Just by the by, you can also make boost tr1 library behave like it's the standard so that when you #include <memory> in vs08, it will actually include boost/tr1/memory.hpp (makes switching between vs10 and vs08 for example transparent) so maybe that may help convince your teammate as well.

See here: http://www.boost.org/doc/libs/1_47_0/doc/html/boost_tr1/usage.html#boost_tr1.usage.include_style

Share this post


Link to post
Share on other sites

Also, I have a teammate who is always trying to stay "pure"...
[/quote]
Purity is simply not a reasonable project goal. Not for projects that will be completed, and do something useful. Professionals and successful amateurs use libraries. Particularly in C++, where the standard library is comparatively bare.

Your team-mate is a liability if they enforce arbitrary rules without sufficient reason. They would have to be a very efficient coder to make up for you re-writing entire subsystems to conform to such ridiculous requirements, and to balance out the additional time spent writing explicit memory management, fixing bugs/memory leaks/memory corruption in such code.

If for social reasons you are forced to accept this requirement (which can happen for professionals too sometimes) then you can always implement your own smart pointer. If your team mate doesn't like this I would advise you ignore them or hand off the subsystem for them to re-write it if they want.

Share this post


Link to post
Share on other sites

Thanks for the input guys, I'll have to look into using Boost classes without installing it.

The Boost license allows you to copy just the smart pointers to your own project and modify them. There's bunch of workarounds in them you can erase to more simplified smart pointers. The license only says you have to include the license text if you distribute the source files.

Boost license


The Boost license permits the creation of derivative works for commercial or non-commercial use with no legal requirement to release your source code. Other differences include Boost not requiring reproduction of copyright messages for object code redistribution, and the fact that the Boost license is not "viral": if you distribute your own code along with some Boost code, the Boost license applies only to the Boost code (and modified versions thereof); you are free to license your own code under any terms you like.
[/quote]

Share this post


Link to post
Share on other sites
I grabbed the Boost library and am working on extracting shared_ptr using the BCP tool. My teammate is a good guy and programmer, he's just a bit obsessed with work only counting when you do it from scratch. We're starting our Senior Project 6 months early, but I'd rather spend the time on architecture than troubleshooting a homemade smart pointer. It's my first stab at a large MVC project, so I've got my work cut out for me.

Thanks for the input guys. I'm glad to know I shouldn't feel like I'm "cutting corners" using the library.

If programming for a good sized game company, what's the likelihood that they'd be using smart pointers? Are they everywhere, or just quarantined to systems that make the most use of them? Also, are you guys using the use std/boost implementations, or in-house versions?

Share this post


Link to post
Share on other sites
I've used both std/boost smart pointers and custom implementations, at various shops; but I've never worked in a C++ programming shop (even outside of games) that didn't make use of smart pointers at some level. How pervasive they are will vary based on development culture, but they're an essential part of writing effective C++ code.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!