std::dereference and std::reference
the stl has standard functors for all operators, like less, greater, equal_to etc.
I find myself needing to reference or dereference objects quite often, and I would have liked functors to operator& and operator*.
While I know I could just write them, I was wondering why they weren't part of the stl.
Is it just because "I guess they forgot", or is there something so dangerous about them, they decided to not put them in? Or something else?
Thoughts?
Re: std::dereference and std::reference
What "objects" are you trying to dereference? As far as I can tell, there's no need for these, as you can easily dereference (or reference) data stored in an STL object, and the same for the STL object itself.
Viggy
Re: std::dereference and std::reference
I was thinking of when I have containers with (or without pointers)
Code:
int main()
{
std::vector<int> intVector(7);
std::vector<int*> intVectorStar;
std::transform(intVector.begin(),
intVector.end(),
std::back_inserter(intVectorStar),
reference<int>());
}
Re: std::dereference and std::reference
Quote:
Originally Posted by
monarch_dodra
I was thinking of when I have containers with (or without pointers)
Code:
int main()
{
std::vector<int> intVector(7);
std::vector<int*> intVectorStar;
std::transform(intVector.begin(),
intVector.end(),
std::back_inserter(intVectorStar),
reference<int>());
}
Well, you could write your own reference template:
Code:
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;
template <typename T>
struct reference
{
typedef T value_type;
typedef T* ptr_type;
ptr_type operator()(value_type& v) const { return &v; }
};
int main()
{
std::vector<int> intVector(7, 0);
std::vector<int*> intVectorStar;
std::transform(intVector.begin(),
intVector.end(),
std::back_inserter(intVectorStar),
reference<int>());
}
Regards,
Paul McKenzie
Re: std::dereference and std::reference
Quote:
Originally Posted by
Paul McKenzie
Well, you could write your own reference template
Yeah, it's not like it is real difficult. The question though was why isn't this function in the stl? Am I the only that feels there would be a use for it?
Re: std::dereference and std::reference
Quote:
Originally Posted by
monarch_dodra
Yeah, it's not like it is real difficult. The question though was why isn't this function in the stl? Am I the only that feels there would be a use for it?
The only thing I can think of is that the STL library's design emphasis is on value-based items and iterators, not explicit pointers. Maybe the authors want to stay as close to this paradigm as possible.
The less. greater, etc. templates are all value-based, so that is the major difference between your additions and the ones that are already in the library.
Regards,
Paul McKenzie
Re: std::dereference and std::reference
Maybe boost::addressof? will do well even if someone decided to overload operator&.