-
April 16th, 2010, 10:02 AM
#1
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?
-
April 16th, 2010, 02:41 PM
#2
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
-
April 18th, 2010, 12:09 PM
#3
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>());
}
-
April 18th, 2010, 05:41 PM
#4
Re: std::dereference and std::reference
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
-
April 18th, 2010, 05:56 PM
#5
Re: std::dereference and std::reference
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?
-
April 18th, 2010, 08:24 PM
#6
Re: std::dereference and std::reference
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
-
April 19th, 2010, 12:59 PM
#7
Re: std::dereference and std::reference
Maybe boost::addressof? will do well even if someone decided to overload operator&.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|