0

I'm trying to figure out what can be achieved with intelligent pointers.
But there something that feels like a hindrance.

A normal pointer has a short definition Someclass *p but intelligent pointers are somewhat longer shared_ptr<SomeClass> p and I feel like it's beginning to get tiresome when you have to deal with templates (like vector) of those pointers.

So it there a short notation for intelligent pointer? Or is there a standard way to allow shorthands?

I know about typedef, but it still feels I'll just end up having plenty of typedef everywhere .

Serge Profafilecebook
  • 1,165
  • 1
  • 14
  • 32
  • 2
    use `auto` from C++11. and most IDE should do autocompletion for you so long name shouldn't make much difference – Bryan Chen Jun 12 '14 at 10:38

1 Answers1

1

C++11 makes this a lot more comfortable!

I use type aliases a lot to combat this problem. This one, in particular, made my code a lot more readable:

template <class T>
using PtrList<T> = std::vector<std::shared_ptr<T>>;

This is only really needed for interfaces though, so you should probably use auto and std::make_shared exclusively in implementations.

When working with or iterating over containers of pointers, it is also very handy to use Boost.Range - expecially boost::adaptors::indirected.

Here's a small example that uses a lot of smart pointers but avoids long types:

PtrList<Bar> foo_to_bar(PtrList<Foo> const& fooList)
{
  PtrList<Bar> resultList;
  resultList.reserve(fooList.size());

  for (auto&& foo : fooList | boost::adaptors::indirected)
  {
    auto bar = std::make_shared<Bar>(foo);
    bar->enableAwesomeness();
    resultList.push_back(bar);
  }

  return resultList;
}
ltjax
  • 15,837
  • 3
  • 39
  • 62