CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    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?

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    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

  3. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    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>());
    }

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: std::dereference and std::reference

    Quote Originally Posted by monarch_dodra View Post
    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

  5. #5
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: std::dereference and std::reference

    Quote Originally Posted by Paul McKenzie View Post
    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?

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: std::dereference and std::reference

    Quote Originally Posted by monarch_dodra View Post
    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

  7. #7
    Join Date
    Aug 2009
    Posts
    81

    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
  •  





Click Here to Expand Forum to Full Width

Featured