- Preserving ref qualifiers in operator-> overload - 1 Update
- std::sort-issue - 3 Updates
- HELP: NEWBIE question on templates. - 18 Updates
Sam <sam@email-scan.com>: May 23 06:39PM -0400 The following doesn't quite do what I want to do: ============================================================================= #include <iostream> struct x { void func() & { std::cout << "lvalue" << std::endl; } void func() && { std::cout << "rvalue" << std::endl; } }; struct y { x *ptr; y(x *ptr) : ptr{ptr} {} x *operator->() & { std::cout << "Deref lvalue" << std::endl; return ptr; } x *&&operator->() && { std::cout << "Deref rvalue" << std::endl; return std::move(ptr); } }; int main() { x X; y Y{&X}; Y->func(); y{&X}->func(); return 0; } ============================================================================= The resulting output is: Deref lvalue lvalue Deref rvalue lvalue What I would like to do is, somehow, an rvalue-qualified operator->() resulting in the rvalue-qualified func() overload getting resolved (i.e., having the "rvalue" overload of func() called). As pictured, x *&& doesn't work. Returning either "x &&*" or "x &&" is ill- formed; is this even possible? |
The Real Non Homosexual <cdalten@gmail.com>: May 22 04:54PM -0700 On Friday, May 22, 2020 at 2:20:55 PM UTC-7, Mr Flibble wrote: > > the second array obviously doesn't happen. So how do I incorporate the > > second array in the sort? I don't see a solution. > Use an intrusive sort. Or if all else fails, use miracle sort. |
Juha Nieminen <nospam@thanks.invalid>: May 23 07:52AM > This is a table sort, you want to sort two columns by a common order. > For that you first sort an array of indices iota(0, N) (complexity > O(N*log(n)), then reorder both columns to the new order (2*O(n)). If you can afford the extra memory needed for an array of indices, you can probably afford the extra memory to simply copy the pairs of values into struct objects, sort it, and assign the values back. No need for the complexities of that index-based sorting and rearrangement. |
Paavo Helde <eesnimi@osa.pri.ee>: May 23 10:59PM +0300 23.05.2020 10:52 Juha Nieminen kirjutas: > of values into struct objects, sort it, and assign the values back. > No need for the complexities of that index-based sorting and > rearrangement. This is the obvious and boring solution which apparently did not satisfy Bonita, so I wanted to offer something more interesting. Anyway, in this case (two columns of small element size) you are probably right. With more columns and larger element size the things might change. Anyway, Bonita's solution to modify std::sort to is still worse because in her solution she also sorts pairs, but the pair elements are disjoint in memory, which basically just means he has slowed down the most expensive step of the algorithm. |
The Real Non Homosexual <cdalten@gmail.com>: May 22 04:39PM -0700 I think referring to a pointed as an iterator can lead to some dangerous and untinded consequences for example, this could spell danger for the following problem... Given an array of non-empty strings, return a Map<String, String> with a key for every different first character seen, with the value of all the strings starting with that character appended together in the order they appear in the array. firstChar(["salt", "tea", "soda", "toast"]) → {"s": "saltsoda", "t": "teatoast"} firstChar(["aa", "bb", "cc", "aAA", "cCC", "d"]) → {"a": "aaaAA", "b": "bb", "c": "cccCC", "d": "d"} firstChar([]) → {} |
The Real Non Homosexual <cdalten@gmail.com>: May 22 04:43PM -0700 By the way, the following problem is a dumber down and simplified variation of a much much hard problem. |
Daniel P <danielaparker@gmail.com>: May 22 05:03PM -0700 On Friday, May 22, 2020 at 5:28:30 PM UTC-4, Jorgen Grahn wrote: > I'd consider changing T to be a random-access iterator; then you'd be > following a pattern used in the standard library. (And pointers are > random-access iterators.) Except with the arrival of ranges, the standard library pattern changes to passing the container, or a view thereon, rather than an iterator pair. Daniel |
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: May 23 12:16AM +0100 On 22/05/2020 23:40, Alf P. Steinbach wrote: > « > A value of a pointer type that is a pointer to or past the end of an object represents the address of the first byte in memory (4.4) occupied b the object 54 or the first byte in memory after the end of the storage occupied by the object, respectively. > » I don't dispute that but the Standard, like me, doesn't call such a pointer an "iterator". /Flibble -- "Snakes didn't evolve, instead talking snakes with legs changed into snakes." - Rick C. Hodgin "You won't burn in hell. But be nice anyway." – Ricky Gervais "I see Atheists are fighting and killing each other again, over who doesn't believe in any God the most. Oh, no..wait.. that never happens." – Ricky Gervais "Suppose it's all true, and you walk up to the pearly gates, and are confronted by God," Byrne asked on his show The Meaning of Life. "What will Stephen Fry say to him, her, or it?" "I'd say, bone cancer in children? What's that about?" Fry replied. "How dare you? How dare you create a world to which there is such misery that is not our fault. It's not right, it's utterly, utterly evil." "Why should I respect a capricious, mean-minded, stupid God who creates a world that is so full of injustice and pain. That's what I would say." |
James Kuyper <jameskuyper@alumni.caltech.edu>: May 22 08:19PM -0700 On Friday, May 22, 2020 at 7:17:05 PM UTC-4, Mr Flibble wrote: > On 22/05/2020 23:40, Alf P. Steinbach wrote: ... > > A value of a pointer type that is a pointer to or past the end of an object represents the address of the first byte in memory (4.4) occupied b the object 54 or the first byte in memory after the end of the storage occupied by the object, respectively. > > » > I don't dispute that but the Standard, like me, doesn't call such a pointer an "iterator". Consider an arbitrary object t. If t is const-qualified, then &t is a constant iterator (27.2.1p4), and therefore doesn't "satisfy the requirements for output iterators". However, if it's not const- qualified, &t is a mutable iterator, and does meet those requirements. Because of the clauses cited by Alf above, 1+&t is a past-the-end iterator that is not dereferenceable (27.2.1p7). Other than that, can you identify even a single iterator requirement listed in section 27.2 that either &t or 1+&t doesn't meet? |
Juha Nieminen <nospam@thanks.invalid>: May 23 07:58AM > foo(v, ...) > foo(x, ...) > Is that possible? Any help or guidance would be most welcome. //======================================================== #include <iostream> #include <vector> template<typename T> void foo(const T& p) { std::cout << "Reference: " << p[0] << "\n"; } template<typename T> void foo(const T* p) { std::cout << "Pointer.\n"; foo(*p); } int main() { std::vector<int> vec(10, 20); const std::vector<int>* ptr = &vec; foo(vec); foo(ptr); } //======================================================== |
Jorgen Grahn <grahn+nntp@snipabacken.se>: May 23 08:26AM On Fri, 2020-05-22, Mr Flibble wrote: > iterators; I find the claim that a pointer to a single object that > is not part of a controlled sequence or an array is an iterator > bogus. I agree, and I suppose that ties in with the "how does foo() know" question. If you pass a single pointer, and no length, what you're passing is probably not an iterator. /Jorgen -- // Jorgen Grahn <grahn@ Oo o. . . \X/ snipabacken.se> O o . |
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: May 23 01:42PM +0100 On 23/05/2020 04:19, James Kuyper wrote: > iterator that is not dereferenceable (27.2.1p7). Other than that, can > you identify even a single iterator requirement listed in section 27.2 > that either &t or 1+&t doesn't meet? I said I didn't dispute that however the Standard clearly states that iterators are abstractions of pointers; a pointer isn't an iterator because a thing can't be an abstraction of itself; the exact wording from the Standard is: 23.3.1 (ISO/IEC JTC1 SC22 WG21 N4860) "Since iterators are an abstraction of pointers, their semantics are a generalization of most of the semantics of pointers in C++. This ensures that every function template that takes iterators works as well with regular pointers." /Flibble -- "Snakes didn't evolve, instead talking snakes with legs changed into snakes." - Rick C. Hodgin "You won't burn in hell. But be nice anyway." – Ricky Gervais "I see Atheists are fighting and killing each other again, over who doesn't believe in any God the most. Oh, no..wait.. that never happens." – Ricky Gervais "Suppose it's all true, and you walk up to the pearly gates, and are confronted by God," Byrne asked on his show The Meaning of Life. "What will Stephen Fry say to him, her, or it?" "I'd say, bone cancer in children? What's that about?" Fry replied. "How dare you? How dare you create a world to which there is such misery that is not our fault. It's not right, it's utterly, utterly evil." "Why should I respect a capricious, mean-minded, stupid God who creates a world that is so full of injustice and pain. That's what I would say." |
James Kuyper <jameskuyper@alumni.caltech.edu>: May 23 07:31AM -0700 On Saturday, May 23, 2020 at 8:42:29 AM UTC-4, Mr Flibble wrote: > > you identify even a single iterator requirement listed in section 27.2 > > that either &t or 1+&t doesn't meet? > I said I didn't dispute that however the Standard clearly states that iterators are abstractions of pointers; a pointer isn't an iterator because a thing can't be an abstraction of itself; the exact wording from the Standard is: The standard says "generalization", not "abstraction". I'm not sure whether that distinction is relevant to your understanding of the situation, but in general a category is a subset of any other category that can properly be described as a generalization of that category. 123456789012345678901234567890123456789012345678901234567890123456789012 For instance, the integers, the rational numbers, the real numbers and the complex numbers are all generalizations of the natural numbers. Each of those categories is a proper subset of the next category I listed - the natural numbers are a proper subset of all of them. The number 2 is in all of those categories. Iterators are a generalization of pointers in exactly the same sense. Every object pointer type is an iterator type. This is not an accident - the requirements for iterator types were deliberately designed to guarantee that object pointer types could satisfy those requirements. Given an unqualified object type T and the following declarations: T t, *p = &t; The only requirements something needs to meet to qualify as an iterator are listed in 27.2.2. p meets all of them:. T* is CopyConstructible T* is CopyAssignable T* Destructible lvalues of type T* are swappable *p is valid if p is dereferenceable p++ is valid Which of those requirements do you claim it doesn't satisfy? Since you restricted your claim to pointers to a single object, I suspect that you might think the last requirement would be a problem, but the clauses Alf cited guarantee that, for the purposes of pointer addition, &t is treated as pointing at the first element of a 1-element array, so p++ is valid expression, which leaves p pointing one past the end of that array. However, p doesn't just meet the general iterator requirements, it also qualifies as a mutable iterator (27.2.1p4), a contiguous iterator (27.2.1p6), an input iterator (27.2.3), an output iterator (27.2.4), a forward iterator (27.2.5), a bidirectional iterator (27.2.6), and a random access iterator (27.2.7). I reiterate my previous challenge: please identify a single iterator requirement that is not satisfied by &t. Footnote 261, while not normative, confirms this interpretation of the normative text: "... pointers are iterators.". All citations are from n4659.pdf, the most recent draft of the standard that I currently have available. Please let me know if anything relevant has changed in the current standard that renders that conclusion inaccurate. |
James Kuyper <jameskuyper@alumni.caltech.edu>: May 23 10:48AM -0400 On 5/23/20 4:26 AM, Jorgen Grahn wrote: > On Fri, 2020-05-22, Mr Flibble wrote: ... >> bogus. > I agree, and I suppose that ties in with the "how does foo() know" > question. I agree that the part of foo() that was provided is poorly designed - but the additional parameters of foo() are unspecified - they might have included either a count or an end-pointer, and the OP didn't think that worth mentioning. > If you pass a single pointer, and no length, what you're > passing is probably not an iterator. ? If the body of a function will be adding or subtracting an integer to the pointer, it needs some way of determining how much it can safely add or subtract - but there's at least two other ways to do that. Provide a second pointer identifying the end of the range, or use a special value in the array to mark the end, such as '\0' for strings. A pointer for which no such method applies should generally be assumed to point at a single object. However, how is that relevant to whether or not the pointer qualifies as an iterator? See my response to Flibble for a more detailed version of that question. |
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: May 23 04:14PM +0100 On 23/05/2020 15:31, James Kuyper wrote: >> I said I didn't dispute that however the Standard clearly states that iterators are abstractions of pointers; a pointer isn't an iterator because a thing can't be an abstraction of itself; the exact wording from the Standard is: > The standard says "generalization", not "abstraction". I'm not sure > whether that distinction is relevant to your understanding of ... <snip> Can't you fucking read or are just deliberately ignoring my reply so you can vomit out a load of drivel? Again, QUOTING FROM THE STANDARD: 23.3.1 (ISO/IEC JTC1 SC22 WG21 N4860) "Since iterators are an abstraction of pointers, their semantics are a generalization of most of the semantics of pointers in C++. This ensures that every function template that takes iterators works as well with regular pointers." Now either learn to fucking read or stop trolling you obtuse egregious cunt. /Flibble -- "Snakes didn't evolve, instead talking snakes with legs changed into snakes." - Rick C. Hodgin "You won't burn in hell. But be nice anyway." – Ricky Gervais "I see Atheists are fighting and killing each other again, over who doesn't believe in any God the most. Oh, no..wait.. that never happens." – Ricky Gervais "Suppose it's all true, and you walk up to the pearly gates, and are confronted by God," Byrne asked on his show The Meaning of Life. "What will Stephen Fry say to him, her, or it?" "I'd say, bone cancer in children? What's that about?" Fry replied. "How dare you? How dare you create a world to which there is such misery that is not our fault. It's not right, it's utterly, utterly evil." "Why should I respect a capricious, mean-minded, stupid God who creates a world that is so full of injustice and pain. That's what I would say." |
"Öö Tiib" <ootiib@hot.ee>: May 23 08:19AM -0700 On Saturday, 23 May 2020 17:48:39 UTC+3, James Kuyper wrote: > However, how is that relevant to whether or not the pointer qualifies as > an iterator? See my response to Flibble for a more detailed version of > that question. I suspect that here is again some kind of wordplay in action. Pointer is usable as iterator but from that does not follow that all pointers are iterators. Null iterators do not exist while null pointers are very widely used. Other common usage of pointers is as pointer to polymorphic base sub-object that also is not valid iterator. |
mehrdad <m-ghassempoory@ntlworld.com>: May 23 05:03PM +0100 On 22/05/2020 22:28, Jorgen Grahn wrote: Many many thanks, I wrote a simple iterator class and after a little bit of wrestling with const and non const conflicts I got everything working. It works a treat! |
mehrdad <m-ghassempoory@ntlworld.com>: May 23 05:16PM +0100 On 23/05/2020 15:48, James Kuyper wrote: It does include a count in my case. |
James Kuyper <jameskuyper@alumni.caltech.edu>: May 23 01:04PM -0400 On 5/23/20 11:19 AM, Öö Tiib wrote: > On Saturday, 23 May 2020 17:48:39 UTC+3, James Kuyper wrote: ... > I suspect that here is again some kind of wordplay in action. > Pointer is usable as iterator but from that does not follow that > all pointers are iterators. Possibly - what do you mean by "iterator"? As far as I can see, the standard doesn't define the term "iterator" - it defines a wide variety of specific iterator categories (several of them in 27.2.1p2, and sporadically throughout section 27) in terms of the iterator type's ability to meet the corresponding requirements, but oddly enough, it doesn't do so for "iterator" itself. To me, an iterator is something that meets the standard's iterator requirements (27.2.2), which all pointer types do. Regardless of what definition you use for "iterator", the standard's definition for, for instance, "random access iterator" does apply, and a pointer to any arbitrary object type fits that definition. The same applies to all of the other iterator categories that the standard does define (in a few cases, such as "constant iterator", that's true only for some pointer types). > ... Null iterators do not exist while null > pointers are very widely used. The existence of null pointers of a given type doesn't have any effect on that type's ability to meet iterator requirements. If the standard's description of a template has a template parameter with a name that ends in "Iterator", it should accept any pointer to object type for that parameter, so long as the values of corresponding objects meet the other requirements of the template. As a general rule, a null pointer won't meet those other requirements, but non-null pointer values should be perfectly usable. The only construct that code associated with such a template might otherwise be able to use to run into trouble is T(). If T is a pointer type, that would result in a null pointer. However, there's no requirement that any iterator type be DefaultConstructible, and as a result, no definition for what the result of such an expression for an iterator type should be. If it results in a pointer value that is not dereferenceable and can't be added to or subtracted from, that doesn't violate any iterator requirements. > ... Other common usage of pointers > is as pointer to polymorphic base sub-object that also is not > valid iterator. A Base* object meets all of the iterator requirements, including the requirement that p++ be a valid expression. Such a pointer is treated as pointing to the first element of a one-element array of Base objects, and p++ points one past the end of that array. |
James Kuyper <jameskuyper@alumni.caltech.edu>: May 23 01:09PM -0400 On 5/23/20 12:16 PM, mehrdad wrote: > On 23/05/2020 15:48, James Kuyper wrote: > It does include a count in my case. As a general rule, when posting a response you should remove any part of the message that you're responding to that isn't directly relevant to your response, and in usenet it's conventional to post your response underneath the thing that it's a response to. It took me a little searching to realize that your message was a response to the following comment: ...>> I agree that the part of foo() that was provided is poorly designed - |
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: May 23 06:22PM +0100 On 23/05/2020 18:09, James Kuyper wrote: > underneath the thing that it's a response to. It took me a little > searching to realize that your message was a response to the following > comment: Do as I say but not as I do, eh, mate? Fucking. Egregious. /Flibble -- "Snakes didn't evolve, instead talking snakes with legs changed into snakes." - Rick C. Hodgin "You won't burn in hell. But be nice anyway." – Ricky Gervais "I see Atheists are fighting and killing each other again, over who doesn't believe in any God the most. Oh, no..wait.. that never happens." – Ricky Gervais "Suppose it's all true, and you walk up to the pearly gates, and are confronted by God," Byrne asked on his show The Meaning of Life. "What will Stephen Fry say to him, her, or it?" "I'd say, bone cancer in children? What's that about?" Fry replied. "How dare you? How dare you create a world to which there is such misery that is not our fault. It's not right, it's utterly, utterly evil." "Why should I respect a capricious, mean-minded, stupid God who creates a world that is so full of injustice and pain. That's what I would say." |
Manfred <noname@add.invalid>: May 23 08:00PM +0200 On 5/23/2020 5:14 PM, Mr Flibble wrote: > Now either learn to fucking read or stop trolling you obtuse egregious > cunt. > /Flibble From n4659 to n4860 the section number is changed (27.2 vs 23.3), however the relevant wording is not: Clause 1 says "Iterators are a generalization of pointers..." Clause 2 says "Since iterators are an abstraction of pointers..." Now you can properly enjoy your fight. |
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page. To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com. |
No comments:
Post a Comment