0

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

2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by sjcomp
0

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;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.