Automatic Reference Counting (ARC) Envy in C++

Many old C++ hacks like me who later learned to do programming in Objective-C watched with envy the introduction of ARC. Well, there is no need for that. C++ has shared_ptr that pretty much mimics ARC. Now, I haven’t tested all possibilities. Certainly, I will like to learn more about circular references and what happens when you store a shared_ptr in a vector or map. But, simple cases certainly work like a charm.

Let’s say we have a class called MyClass.

class MyClass {
		cout << "Creating MyClass object" << endl;
	virtual ~MyClass(){
		cout << "Destroying MyClass object" << endl;
	void method(){
		cout << "Called method of MyClass object" << endl;

Below is how we can manage an object of MyClass using automatic reference counting.

#include <memory>

using namespace std;

shared_ptr<MyClass> returnTest() {
	shared_ptr<MyClass> m(new MyClass);


	return m;

int main() {
	shared_ptr<MyClass> m2 = returnTest();


	return 0;

This is a pretty good test. An object is created from a method and returned from there. The return value is assigned to a variable. The output from this program will be:

Creating MyClass object
Called method of MyClass object
Called method of MyClass object
Destroying MyClass object

The behavior is no different from Objective-C. Except that in Objective-C, the returned object will be put on an auto release pool.

On another note, I had to add the –std=c++0x option to GNU++ compiler for shared_ptr to work.

3 thoughts on “Automatic Reference Counting (ARC) Envy in C++

  1. The level of syntactic “junk” required in C++ has become mind boggling. What used to be a relatively simple language has become cluttered. Contrast that with Objective-C which instead of relying on “clever templates” uses a clever compiler – the contrast is breath taking.

    Why must I, in the 21st century, declare a pointer “shared_ptr xyz”? This points out an inherent defect in C++ programming (and programmers who accept this as being valid). Templates do not ameliorate the issue, they exacerbate it.

    Breaking it down, this doesn’t simplify anything. Anyone who declares MyClass *foo = new MyClass(); without a delete is going to get a leak. And despite the most diligent coder, this is going to happen.

    Rather than solve the problem, once again, templates just exacerbate it. ObjC with ARC allows me to have “MyClass *foo” and not worry about a leak. C++ does not.

    Code behavior and programmer behavior are two very different things. The more onerous a task you make ARC (and using templates to provide such a basic function IS onerous), the less people will use it. It is one thing to provide it for “free” (as in the user never sees it) and quite a different one to say “oh, you can use it, but the very nature of your code will look different”.

    That’s not better. That’s not good design. That’s a hack. A clever hack, but still a hack. Worse, it FEELS like a hack.

    • Thank you for your comment. I agree with your observation about “junk” in C++. However, I will not go about calling Objective-C any better. ARC continues to use a very simplistic reference count based model. This does not mitigate all issues. Only this week I ran into one. Garbage collection, on the other hand, traverses the full object graph and decides which objects need to be released and is a much more rigorous approach. Technically, Objective-C has GC but not in iOS yet. Also, ARC continues to rely on auto release pool. Where as, shared_ptr in C++ doesn’t need that.

      Both are ancient languages. At least, there is some innovation still going on in Objective-C, thanks to Apple. C++ has no such steward.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s