Friday, May 22, 2020

Digest for comp.lang.c++@googlegroups.com - 10 updates in 3 topics

mehrdad <m-ghassempoory@ntlworld.com>: May 22 08:04PM +0100

I would like to write a template function that approximately looks like:
 
template<class T>
void foo (const T &p, ....)
{
...
...
}
 
where the only requirement for p is that it must have operator []
defined for it, however I would like this function also work when p is a
pointer to type T, in other words I want both of these calls to work
with the same template function:
 
std::vector<double> v;
double *p;
 
foo(v, ...)
foo(x, ...)
 
Is that possible? Any help or guidance would be most welcome.
Jorgen Grahn <grahn+nntp@snipabacken.se>: May 22 09:28PM

On Fri, 2020-05-22, mehrdad wrote:
 
> foo(v, ...)
> foo(x, ...)
 
> Is that possible? Any help or guidance would be most welcome.
 
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.)
 
But how does foo() know which arguments to operator[] are safe, when
you don't pass a container like vector<double>?
 
/Jorgen
 
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: May 22 11:06PM +0100

On 22/05/2020 22:28, Jorgen Grahn wrote:
> random-access iterators.)
 
> But how does foo() know which arguments to operator[] are safe, when
> you don't pass a container like vector<double>?
 
Nit: a pointer *might be* an iterator but not all pointers are 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.
 
/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."
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: May 23 12:40AM +0200

On 23.05.2020 00:06, Mr Flibble wrote:
 
> Nit: a pointer *might be* an iterator but not all pointers are
> 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.
 
C++ has always supported using pointers to single objects as iterators.
 
You can form a pointer to past-the-object just as with an array.
 
C++17 §8.3.1/3
«
For purposes of pointer arithmetic (8.7) and comparison (8.9, 8.10), an
object that is not an array element whose address is taken in this way
is considered to belong to an array with one element of type T. [Example:
struct A { int i; };
struct B : A { };
... &B::i ... // has type int A::*
int a;
int* p1 = &a;
int* p2 = p1 + 1; // defined behavior
bool b = p2 > p1; // defined behavior, with value true
—end example]
»
 
Also,
 
C++17 §6.9.2/3.4
«
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.
»
 
 
- Alf
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."
Juha Nieminen <nospam@thanks.invalid>: May 22 04:32PM

> But if I give std::sort the pointers to the first array the swapping on
> the second array obviously doesn't happen. So how do I incorporate the
> second array in the sort? I don't see a solution.
 
The easiest solution would be, of course, if you can change that
structure to have the uintptr_t's and the pointers adjacent to
each other (ie. have them as members of a struct. Then you can
simply have a comparison operator for the uintptr_t member of
the struct and the rest will happen automatically).
 
If you can't change the data structure to be like that, and it must
remain as it is, then the question becomes whether you can use
additional memory to perform this sorting or not.
 
If you can, then it becomes equally easy: Just create a temporary
array with such structs, copy the uintptr_t values and the pointers
to these struct objects, sort it, and the copy them back to their
new places.
 
If you absolutely cannot afford to use any extra memory for this,
then it becomes a bit more complicated. One alternative to do it
in-place with no extra memory is to implement your own sorting
which handles the data to be sorted as pairs of values separated
by an offset. (I'm not sure if std::sort() itself could somehow
be used in this manner. Probably not.)
Bonita Montero <Bonita.Montero@gmail.com>: May 22 08:41PM +0200

I copied a std::sort implementation and added a swap template-parameter.
This is feeded with a lambda which knows the offsets between the tables.
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: May 22 10:31PM +0200

On 22.05.2020 18:32, Juha Nieminen wrote:
> which handles the data to be sorted as pairs of values separated
> by an offset. (I'm not sure if std::sort() itself could somehow
> be used in this manner. Probably not.)
 
I'm pretty sure `std::sort` can do it with custom iterators that when
dereferenced produce proxy objects that each represent a pair of items.
 
However, it's a heck of a lot of code to support an unlikely scenario,
and even though `std::sort` supports optimizations based on inlining I
somehow doubt that it will be as efficient as sorting indices.
 
The case has at least two sources of unpleasant odour: using effectively
parallel arrays (even though joined into one physical array), and
representing pointers with `intptr_t` values. Instead of using time on
adapting sorting to that, I would spend the time fixing the design
issues that have caused the odours. And then just use `std::sort`.
 
- Alf
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: May 22 10:20PM +0100

On 21/05/2020 14:42, Bonita Montero wrote:
> But if I give std::sort the pointers to the first array the swapping on
> 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.
 
/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."
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: May 22 05:38AM +0100

Hi!
 
My ECS is now completely free: I have moved it from neoGFX to neolib and its licence has changed from GPLv3 to a permissive licence. neolib now uses CMake and builds on Linux. #cpp #gamedev #coding #neoGFX
 
https://github.com/i42output/neolib/tree/master/include/neolib/ecs
 
/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."
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: