Hello, I have a vector of smart pointers and I'd like to NULL smart pointers in reverse order. Smart pointers are done with boost::shared_ptr<>, this is the code:

// List of pointers
typedef boost::shared_ptr<int> TSmart;
std::vector<TSmart> Vec;
// Make sure that everything is NULL
std::transform( Vec.rbegin(), Vec.rend(), TSmart());

I get errors for this one. What am I doing wrong? I tried to use boost::bind

std::transform( Vec.rbegin(), Vec.rend(), boost::bind<TSmart>(TSmart()));

But I still get errors. I do not want to use boost::lambda there. What is the proper way to do that?
Thanks

Well, if you use vec.size() surely the vector is empty when you're trying to null everything... right? Also, doesn't transform take four parameters?

Anyway, something like this should work:

#include <iostream>
#include <vector>
#include <algorithm>

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

template <class ty>
  class nullify {
  private:
  public:
    ty operator ( ) ( ty &elem ) {
      elem.reset();
      return elem;
    }
  };

int main() {
  // List of pointers
  typedef boost::shared_ptr<int> TSmart;

  std::vector<TSmart> Vec;

  // Make sure that everything is NULL
  std::transform( 
    Vec.begin(), 
    Vec.end(), 
    Vec.begin(), 
    nullify<TSmart>() );

  return 0;
}

Thanks a lot, twomers. Your solution works!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.