Containers of references?

140 views
Skip to first unread message

NDos Dannyu

unread,
Jan 3, 2017, 7:57:54 PM1/3/17
to ISO C++ Standard - Future Proposals
 I tried to make multiplex of std::set, named NDos::set_multiplex, which can view the elements in perspective of various comparison objects. For example, a set of playing cards could be sorted with rank first and suit second, or suit first and rank second; NDos::set_multiplexenables to do this conveniently. NDos::set_multiplex does this by inheriting a std::set storing the elements, and multiple std::multisets storing the iterator to the elements.
 But I got a problem: I couldn't implement erasure method properly. It should be able to erase an element in perspective of any comparison object, but it can't. The ones storing the iterator to the elements expects the iterator to an element to be erased, and it is impossible to acquire such an iterator.
 So I specialized my own implementation of std::multiset to be able to store references, and I replaced the std::multisets by them.
 And I request this feature to be added in the standard. It also could be done for std::liststd::map std::unordered_set, etc.

Thiago Macieira

unread,
Jan 3, 2017, 8:40:34 PM1/3/17
to std-pr...@isocpp.org
On terça-feira, 3 de janeiro de 2017 16:57:53 BRST NDos Dannyu wrote:
> So I specialized my own implementation of std::multiset to be able to
> store references, and I replaced the std::multisets by them.
> And I request this feature to be added in the standard. It also could be
> done for std::list, std::map, std::unordered_set, etc.

You can do that with a container of pointers, only that you have to
dereference the pointer before accessing the data. I think this conveys more
information, including the fact that the ownership of the data is unclear and
that the container doesn't own anything at all.

--
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
Software Architect - Intel Open Source Technology Center

Tony V E

unread,
Jan 3, 2017, 9:05:30 PM1/3/17
to ISO C++ Standard - Future Proposals
Have you looked at boost::multi_index?

Sent from my BlackBerry portable Babbage Device
From: NDos Dannyu
Sent: Tuesday, January 3, 2017 7:57 PM
To: ISO C++ Standard - Future Proposals
Subject: [std-proposals] Containers of references?

--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/b20edd34-15bb-453c-be8d-c21a83a90fdf%40isocpp.org.

arthur...@mixpanel.com

unread,
Jan 4, 2017, 6:49:47 PM1/4/17
to ISO C++ Standard - Future Proposals
On Tuesday, January 3, 2017 at 4:57:54 PM UTC-8, NDos Dannyu wrote:
 I tried to make multiplex of std::set, named NDos::set_multiplex, which can view the elements in perspective of various comparison objects. For example, a set of playing cards could be sorted with rank first and suit second, or suit first and rank second; NDos::set_multiplexenables to do this conveniently. NDos::set_multiplex does this by inheriting a std::set storing the elements, and multiple std::multisets storing the iterator to the elements.
 But I got a problem: I couldn't implement erasure method properly. It should be able to erase an element in perspective of any comparison object, but it can't. The ones storing the iterator to the elements expects the iterator to an element to be erased, and it is impossible to acquire such an iterator.

Did you try just
    std::set<std::reference_wrapper<S>, std::less<S>> myset;
    myset.insert(std::ref(myobject));
?
That works for me, and AFAICT it doesn't create any excess temporaries anywhere.

–Arthur

P.S.: Feel free to take this to StackOverflow and post a link in this thread. :)

NDos Dannyu

unread,
Jan 4, 2017, 11:39:55 PM1/4/17
to ISO C++ Standard - Future Proposals, arthur...@mixpanel.com


2017년 1월 5일 목요일 오전 8시 49분 47초 UTC+9, arthur...@mixpanel.com 님의 말:
Did you try just
    std::set<std::reference_wrapper<S>, std::less<S>> myset;
    myset.insert(std::ref(myobject));
?
That works for me, and AFAICT it doesn't create any excess temporaries anywhere.

–Arthur

P.S.: Feel free to take this to StackOverflow and post a link in this thread. :)
No. I just tried std::multiset<T&>. And of course it failed, because there is no allocator of references.
And this thread is actually from Stack Overflow question I posted: http://stackoverflow.com/questions/41392779/parameter-pack-expansion-doesnt-work
( Bad rating, :( )

D. B.

unread,
Jan 5, 2017, 3:36:11 AM1/5/17
to std-pr...@isocpp.org
Arrays and containers of references are forbidden altogether, as a fundamental rule - not because there's no allocator written for references yet.

If you had an array or container of references, there'd be no way to tell whether an indexing or arithmetic operation should act upon the element (or its storage) - or the thing to which it referred. (Using a struct that itself has a reference as its only member works around this because then you must disambiguate the names.) There are other reasons, but this is perhaps the most convincing one to me - and the one I see the least.

NDos Dannyu

unread,
Jan 6, 2017, 1:02:39 AM1/6/17
to ISO C++ Standard - Future Proposals
2017년 1월 4일 수요일 오전 10시 40분 34초 UTC+9, Thiago Macieira 님의 말:
You can do that with a container of pointers, only that you have to 
dereference the pointer before accessing the data. I think this conveys more 
information, including the fact that the ownership of the data is unclear and 
that the container doesn't own anything at all. 
It turns out it is also impossible to acquire such pointer. 

2017년 1월 5일 목요일 오후 5시 36분 11초 UTC+9, D. B. 님의 말:
Arrays and containers of references are forbidden altogether, as a fundamental rule - not because there's no allocator written for references yet.

If you had an array or container of references, there'd be no way to tell whether an indexing or arithmetic operation should act upon the element (or its storage) - or the thing to which it referred. (Using a struct that itself has a reference as its only member works around this because then you must disambiguate the names.) There are other reasons, but this is perhaps the most convincing one to me - and the one I see the least.
Then, we can just use different identifier for container of references. 

NDos Dannyu

unread,
Jan 6, 2017, 1:20:42 AM1/6/17
to ISO C++ Standard - Future Proposals
...I'll try using std::reference_wrapper, anyway.

D. B.

unread,
Jan 6, 2017, 3:43:37 AM1/6/17
to std-pr...@isocpp.org
On Fri, Jan 6, 2017 at 6:02 AM, NDos Dannyu <ndosp...@naver.com> wrote:
Then, we can just use different identifier for container of references. 

I don't know what you mean, but either way, I don't think it's as easy/possible as you're implying.

But by all means, if you figure out a way to implement this in an existing compiler, please post the results.

--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.

To post to this group, send email to std-pr...@isocpp.org.

NDos Dannyu

unread,
Jan 7, 2017, 7:14:55 AM1/7/17
to ISO C++ Standard - Future Proposals


2017년 1월 6일 금요일 오후 5시 43분 37초 UTC+9, D. B. 님의 말:
But by all means, if you figure out a way to implement this in an existing compiler, please post the results.
 I finally managed to implement this. I made the compare object to compare equal objects equal only if I am searching over the container, not inserting to or erasing from the container, and that made erasing a single element of std::multimap possible.
 Unfortunately, I lost any usage of this container. There is much better and elegant way to do I want. Actually, I am making a pattern searcher of Conways Game of Life. Rather than having multiple associative containers anytime, I can just copy or move elements from std::set to std::multiset only if needed.
 So, I am withdrawing this discussion, and I am not going to show the result of a horrible waste of time.
Reply all
Reply to author
Forward
0 new messages