Now I want a 0-parameter method from std::string to be called on every element from the sequence. I tried to use std::mem_fun (as above), but it claims as follows:
>> cannot convert parameter 1 from 'boost::shared_ptr<T>' to 'const std::basic_string<_Elem,_Traits,_Ax> *'
The question is - how to construct a correct adapter for it, not using a custom operand class. I want to use only stl stuff.
I don't know how you can solve the problem, but at a first glance I'm afraid that a smart pointer (boost::shared_ptr in your case) cannot be used where a "real" pointer is needed (but I'm not sure).
The compiler tells that he expects an object of type 'const std::string *' (std::string is just an alias for std::basic_string<char>), he found an object of type boost::shared_ptr<string>.
I feel that a smart pointer acts _logically_ as a pointer, but _is not_ a pointer...
Maybe you've already tried, but I remark that your code compiles fine if you substitute boost::shared_ptr<std::string> with string*, i.e.
> Now I want a 0-parameter method from std::string to be called on every > element from the sequence. I tried to use std::mem_fun (as above), but > it claims as follows:
> >> cannot convert parameter 1 from 'boost::shared_ptr<T>' to 'const std::basic_string<_Elem,_Traits,_Ax> *'
> The question is - how to construct a correct adapter for it, not using > a custom operand class. I want to use only stl stuff.
Since you're using Boost (or TR1), you can use boost::mem_fn (or std::tr1::mem_fn) or boost::bind (or std::tr1::bind):
> Maybe you've already tried, but I remark that your > code compiles fine if you substitute > boost::shared_ptr<std::string> with string*, i.e.
[snip]
Sure, but one of the great things about shared_ptr (and some other smart pointers) is that it allows easy use of pointers in STL containers. Switching to raw pointers eliminates automatic cleanup (especially important in the face of exceptions), shared ownership, etc., which the OP may need. Using RAII techniques, like that implemented by shared_ptr, is one of the best ways to eliminate memory (and other resource) leaks. See Sutter and Alexandrescu, _C++ Coding Standards_, Item 13.
> On Aug 20, 10:06 am, Giovanni Gherdovich<gherdov...@students.math.unifi.it> wrote:
> [snip]> Maybe you've already tried, but I remark that your > > code compiles fine if you substitute > > boost::shared_ptr<std::string> with string*, i.e.
> [snip]
> Sure, but one of the great things about shared_ptr (and some other > smart pointers) is that it allows easy use of pointers in STL > containers. Switching to raw pointers eliminates automatic cleanup > (especially important in the face of exceptions), shared ownership, > etc., which the OP may need. Using RAII techniques, like that > implemented by shared_ptr, is one of the best ways to eliminate memory > (and other resource) leaks. See Sutter and Alexandrescu, _C++ Coding > Standards_, Item 13.
> Cheers! --M
> Cheers! --M
Thanks
I haven't considered boost::mem_fun_ref. It works. In terms of string*, it was an example, I actually use more havy classes in my containers but for simplicity provided an example with shared_ptr<string>.
On Aug 20, 5:20 pm, mlimber <mlim...@gmail.com> wrote:
> Sure, but one of the great things about shared_ptr (and some other > smart pointers) is that it allows easy use of pointers in STL > containers. Switching to raw pointers eliminates automatic cleanup > (especially important in the face of exceptions), shared ownership, > etc., which the OP may need. Using RAII techniques, like that > implemented by shared_ptr, is one of the best ways to eliminate memory > (and other resource) leaks. See Sutter and Alexandrescu, _C++ Coding > Standards_, Item 13.
Thank you for the reference to Sutter & Alexandrescu!