While transform iterator implementations such as boost::transform_iterator
are certainly useful, all implementations I've found seem to share one common weakness: they only provide operator*
, and no operator->
.
Given a transform function returning a reference, implementing operator->
is straightforward.
Given a transform function returning by value, implementing operator->
using a proxy should also not be a problem.
Why is it deemed preferable to provide only an operator*
, and no operator->
? Is the use of a proxy here any different from e.g. std::vector<bool>
?
Update: Here's a very simple proof of concept to demonstrate how such a proxy object could work (hard-coded for std::string
type):
#include <iostream>
#include <functional>
#include <string>
class StringProxyDemo {
public:
class Proxy {
public:
Proxy(std::string value) : _value(std::move(value)) { }
std::string *operator->() { return &_value; }
private:
std::string _value;
};
Proxy operator->() { return _function(); }
StringProxyDemo(std::function<std::string()> function)
: _function(std::move(function)) { }
private:
std::function<std::string()> _function;
};
int main() {
StringProxyDemo demo(
[]() -> std::string {
return "Hello, World!";
}
);
std::cout << demo->size() << std::endl;
std::cout << demo->c_str() << std::endl;
}