Friday, January 3, 2020

Digest for comp.lang.c++@googlegroups.com - 25 updates in 5 topics

boltar@nowhere.org: Jan 03 02:53PM

On Thu, 2 Jan 2020 22:20:28 -0500
>definition in any version of the C++. That absence is rather odd, if
>you're going to insist that such behavior is the main reason for the
>existence of unions.
 
Not stated doesn't make it illegal. Its not stated anywhere that 1+1 = 2
anywhere in the standard either funnily enough.
Robert Wessel <robertwessel2@yahoo.com>: Jan 03 10:09AM -0600


>>MSVC.
 
>Fair enough, but last time I used it a few years back MSVC didn't support some
>features of modern C++ either.
 
 
An important difference is that MS for a long time has intended to
fully support modern C++ (that it often took some time get everything
done is not really relevant), but has explicitly decided *not* to
support C99 and later. That's changed a smidgen, of late, and will
VLAs no longer being mandatory, we might plausibly see more modern C
support, but still without VLAs.
David Brown <david.brown@hesbynett.no>: Jan 03 05:22PM +0100

>> you're going to insist that such behavior is the main reason for the
>> existence of unions.
 
> Not stated doesn't make it illegal.
 
It makes it undefined behaviour. It's kind of obvious, really - if the
behaviour has not been defined by the standards, it is undefined.
 
> Its not stated anywhere that 1+1 = 2
> anywhere in the standard either funnily enough.
 
Actually, it is.
 
Section 6.5.6p5 of the C standard says "The result of the binary +
operator is the sum of the operands." There will be an equivalent
definition in the C++ standard if you choose to look for it.
 
 
At what point will you realise you'll benefit more by trying to learn
from other people, rather than continually making a fool of yourself?
You are ignorant of a great deal about how C++ works, in terms of the
language, the standards, compilers, and practical usage. Fair enough -
there is nothing wrong with ignorance (we are all ignorant about most
things), and it is curable by listening, reading and learning. It is
your insistence that you know what you are talking about that is the
problem - as you get things wrong again, and again. Insulting people
won't make you right - it just makes you appear more ridiculous.
James Kuyper <jameskuyper@alumni.caltech.edu>: Jan 03 09:01AM -0800

Bonita Moreno said
> >omits any explicit definition of behavior" (1.3.25).
> ...
 
> Not stated doesn't make it illegal. ...
 
Correct, and I've said nothing to suggest otherwise. Nothing is illegal
as far as the C++ standard is concerned. The absolute worst thing that
the standard says about any code is "undefined behavior". The standard
imposes no requirements on code with undefined behavior. In particular,
it does not require that the author be jailed or fined (which is what
it would mean to call it illegal). More importantly, it also doesn't
require that the code be rejected, nor does it require that the user
be warned.
And, per 1.3.25, cited above, if "Not stated" qualifies as omission of
any explicit definition of the behavior, then "Not stated" is, quite
explicitly, sufficient to make the behavior undefined.
 
> ... Its not stated anywhere that
> 1+1 = 2 anywhere in the standard either funnily enough.
 
True - but in this case the fact that it isn't stated does not mean
that there's no explicit definition of the behavior. The explicit
definition of the behavior doesn't have to be a complete list of every
single case, and usually isn't. It can be, and usually is, a single
general rule that happens to cover the specific case in question. In
this case, there are two such rules: "The result of the binary +
operator is the sum of the operands" (8.7p2) and "If two operands
compare equal, the result is true for the == operator ... If the two
operands compare false, the result is false for the == operator ..."
(8.10p5).
 
So - what general rule does the C++ standard state that covers the
specific case if reading from a member of a union that is not
currently the active one?
Keep in mind that the relevant clause is says that "the value
contained in the object indicated by the glvalue is the prvalue
result." (7,1p3). The members of a union are considered to be
distinct objects that happen to have the same location in memory.
Therefore, if the member being read is not the active one, there is
no value contained in that object, and 7.1p3 doesn't apply.
The C standard contains a non-normative footnote which claims that
the contents of that memory get reinterpreted as if they contained an
object of the same type as the lvalue used to read it. The C++
committee has known about the C wording for more than two decades
now, and has chosen to not incorporate similar wording in any version
of the C++ standard. I think we can safely assume that this omission
constitutes a deliberate difference between the two languages,
probably due to the greater importance of type safety in C++. It's
hard to imagine anything more type-unsafe than type punning.
Frederick Gotham <cauldwell.thomas@gmail.com>: Jan 03 07:04AM -0800

My GNU C++ compiler version 7.4.0 for Ubuntu won't compile the following code and I don't know why.
 
If I remove the second template parameter, i.e. "size_t capacity", then it works fine.
 
I wonder if another compiler will compile it?
 
This code is for a new kind of allocator that uses global (i.e. static duration) memory.
 
#include <cstddef> /* size_t */
#include <new> /* Only for bad_alloc */
 
template<typename T, std::size_t capacity>
class StaticAllocator {
public:
typedef T value_type;
 
protected:
 
static T buf[capacity];
 
public:
 
T *allocate(std::size_t const n)
{
if (n > capacity)
throw std::bad_alloc();
 
return buf;
}
 
void deallocate(T *, std::size_t)
{
/* Do Nothing */
}
};
 
template<typename T, std::size_t capacity>
T StaticAllocator<T,capacity>::buf[capacity];
 
using std::size_t;
 
#include <vector>
using std::vector;
 
#include <iostream>
using std::cout;
using std::endl;
 
auto main(void) -> int
{
vector< char, StaticAllocator<char, 4> > v;
 
v.push_back('a');
v.push_back('b');
v.push_back('c');
v.push_back('d');
 
for (auto const &elem : v)
cout << elem << endl;

vector< char, StaticAllocator<char, 4> > v2;
 
v2.push_back('x');
v2.push_back('y');
v2.push_back('z');
 
for (auto const &elem : v2)
cout << elem << endl;

// Now try the first vector again

for (auto const &elem : v)
cout << elem << endl;
}
Melzzzzz <Melzzzzz@zzzzz.com>: Jan 03 04:57PM

> };
 
> template<typename T, std::size_t capacity>
> T StaticAllocator<T,capacity>::buf[capacity];
 
can't check but this is missing initializer?
just say buf and it will compile.
 
 
> for (auto const &elem : v)
> cout << elem << endl;
> }
 
What's the purpose of allocator when you can allocate only one
vector with it?
 
--
press any key to continue or any other to quit...
U ničemu ja ne uživam kao u svom statusu INVALIDA -- Zli Zec
Svi smo svedoci - oko 3 godine intenzivne propagande je dovoljno da jedan narod poludi -- Zli Zec
Na divljem zapadu i nije bilo tako puno nasilja, upravo zato jer su svi
bili naoruzani. -- Mladen Gogala
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Jan 03 02:04PM

On 03/01/2020 08:40, Öö Tiib wrote:
> at same time. That also does confuse me. For me it is not question if it
> is defined behavior but uncertainty about why I need it regardless if it
> is defined behavior or not.
 
I need this ability so I can have a swizzle class.
 
/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."
David Brown <david.brown@hesbynett.no>: Jan 03 03:24PM +0100

On 03/01/2020 15:04, Mr Flibble wrote:
 
 
> I need this ability so I can have a swizzle class.
 
What exactly do you mean by "swizzle class", and what are you trying to do?
 
I am entirely confident that you do /not/ need union type punning to
achieve your goals. But it is possible that it would simpler, clearer
and more efficient than standards-compliant solutions. (This is fine,
of course, if you are happy to restrict your code to compilers for which
the code is valid.) Without more details of what you are trying to do,
I don't see how anyone can help - another thread trying to convince
people to understand what unions can and cannot do in standard C++ is
not particularly useful.
boltar@nowhere.org: Jan 03 02:46PM

On Thu, 2 Jan 2020 21:12:40 +0100
>"Here traits_type::copy contains a call to memcpy, which is optimized
>into a single register copy of the whole string (carefully selected to
>fit). The compiler also transforms a call to strlen into a compile time 8."
 
Great. And if the string is say 150 bytes long?
"Öö Tiib" <ootiib@hot.ee>: Jan 03 06:47AM -0800

On Friday, 3 January 2020 16:04:31 UTC+2, Mr Flibble wrote:
> > is defined behavior but uncertainty about why I need it regardless if it
> > is defined behavior or not.
 
> I need this ability so I can have a swizzle class.
 
Ok. Then you need to make your swizzle class "standard layout class"
([class] 7) with all of non-static data members being "common initial
sequence".
 
class.mem] 23
"In a standard-layout union with an active member (12.3) of struct
type T1, it is permitted to read a non-static data member m of
another union member of struct type T2 provided m is part of the
common initial sequence of T1 and T2; the behavior is as if the
corresponding member of T1 were nominated."
 
And from [class.union] I have strong impression that writing into
such union member just ends lifetime of previous active member
and starts lifetime of written into active member without any
cleanup or initializations done.
boltar@nowhere.org: Jan 03 02:49PM

On Thu, 2 Jan 2020 21:54:05 +0000
>More to the point, in gcc/clang memcpy is a built-in, and in VS an
 
Built-in doesn't mean loop free and no stack push/pops. It just means the
compiler doesn't have to link in code from a library but can generate it on
the spot.
 
>arguing with this guy: from his repetitive posts, he is either dim or a
>dick, possibly both.
 
Ad Hominem seems to be a common theme from you assclowns when you can't
actually argue your point.
boltar@nowhere.org: Jan 03 02:55PM

On Fri, 3 Jan 2020 02:21:28 -0800 (PST)
 
>> >Experiments of mine and others have shown that usage of memcpy
 
>> Lets see your test code then.
 
>You first. You demonstrated lack of knowledge with the "overhead of a
 
You made the claim so either back it up or fuck off back under your bridge.
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Jan 03 02:57PM

On 03/01/2020 14:47, Öö Tiib wrote:
> such union member just ends lifetime of previous active member
> and starts lifetime of written into active member without any
> cleanup or initializations done.
 
No that won't help me with my swizzle class.
 
/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."
Bonita Montero <Bonita.Montero@gmail.com>: Jan 03 04:04PM +0100

>> You first. You demonstrated lack of knowledge with the "overhead of a
 
> You made the claim so either back it up or fuck off back under your bridge.
 
I just did
 
#include <cstring>
#include <cstdint>
 
uint64_t native( double d )
{
#if defined(UNION)
union
{
double du;
uint64_t dx;
};
du = d;
return dx;
#else
uint64_t n;
memcpy( &n, &d, sizeof(n) );
return n;

No comments: