Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #70942 > unrolled thread

Union type punning in C++

Started byMr Flibble <flibbleREMOVETHISBIT@i42.co.uk>
First post2020-01-01 19:24 +0000
Last post2020-01-13 16:29 -0800
Articles 20 on this page of 135 — 18 participants

Back to article view | Back to comp.lang.c++


Contents

  Union type punning in C++ Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2020-01-01 19:24 +0000
    Re: Union type punning in C++ boltar@nowhere.org - 2020-01-02 09:36 +0000
    Re: Union type punning in C++ Öö Tiib <ootiib@hot.ee> - 2020-01-02 03:34 -0800
      Re: Union type punning in C++ Bo Persson <bo@bo-persson.se> - 2020-01-02 12:49 +0100
        Re: Union type punning in C++ Öö Tiib <ootiib@hot.ee> - 2020-01-02 04:52 -0800
          Re: Union type punning in C++ boltar@nowhere.org - 2020-01-02 17:22 +0000
            Re: Union type punning in C++ Öö Tiib <ootiib@hot.ee> - 2020-01-02 10:26 -0800
              Re: Union type punning in C++ Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2020-01-02 21:54 +0000
                Re: Union type punning in C++ boltar@nowhere.org - 2020-01-03 14:49 +0000
                  Re: Union type punning in C++ David Brown <david.brown@hesbynett.no> - 2020-01-03 17:11 +0100
                  Re: Union type punning in C++ Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2020-01-03 17:55 +0000
              Re: Union type punning in C++ boltar@nowhere.org - 2020-01-03 09:55 +0000
                Re: Union type punning in C++ David Brown <david.brown@hesbynett.no> - 2020-01-03 11:20 +0100
                Re: Union type punning in C++ Öö Tiib <ootiib@hot.ee> - 2020-01-03 02:21 -0800
                  Re: Union type punning in C++ boltar@nowhere.org - 2020-01-03 14:55 +0000
                    Re: Union type punning in C++ Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-03 16:04 +0100
                      Re: Union type punning in C++ "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-01-03 14:45 -0800
                        Re: Union type punning in C++ Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-04 01:26 +0100
                          Re: Union type punning in C++ "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-01-03 16:55 -0800
                            Re: Union type punning in C++ Melzzzzz <Melzzzzz@zzzzz.com> - 2020-01-04 01:02 +0000
                              Re: Union type punning in C++ "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-01-03 17:06 -0800
                    Re: Union type punning in C++ Öö Tiib <ootiib@hot.ee> - 2020-01-03 07:34 -0800
                      Re: Union type punning in C++ boltar@nowhere.org - 2020-01-03 16:12 +0000
                        Re: Union type punning in C++ Öö Tiib <ootiib@hot.ee> - 2020-01-03 08:49 -0800
                        Re: Union type punning in C++ Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-03 19:28 +0100
                          Re: Union type punning in C++ Melzzzzz <Melzzzzz@zzzzz.com> - 2020-01-03 18:54 +0000
                            Re: Union type punning in C++ Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-04 01:25 +0100
                              Re: Union type punning in C++ Melzzzzz <Melzzzzz@zzzzz.com> - 2020-01-04 00:28 +0000
                                Re: Union type punning in C++ Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-04 01:41 +0100
                                  Re: Union type punning in C++ Melzzzzz <Melzzzzz@zzzzz.com> - 2020-01-04 00:52 +0000
                                    Re: Union type punning in C++ Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-04 08:08 +0100
                                      Re: Union type punning in C++ Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-04 08:34 +0100
                                        Re: Union type punning in C++ Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-04 08:43 +0100
                                        Re: Union type punning in C++ Melzzzzz <Melzzzzz@zzzzz.com> - 2020-01-04 08:20 +0000
                                          Re: Union type punning in C++ Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-04 09:25 +0100
                                      Re: Union type punning in C++ Melzzzzz <Melzzzzz@zzzzz.com> - 2020-01-04 08:25 +0000
                                        Re: Union type punning in C++ Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-04 09:25 +0100
                                          Re: Union type punning in C++ Melzzzzz <Melzzzzz@zzzzz.com> - 2020-01-04 08:27 +0000
                                            Re: Union type punning in C++ Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-04 09:36 +0100
                                              Re: Union type punning in C++ Melzzzzz <Melzzzzz@zzzzz.com> - 2020-01-04 08:41 +0000
                                                Re: Union type punning in C++ Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-04 09:49 +0100
                                                  Re: Union type punning in C++ Melzzzzz <Melzzzzz@zzzzz.com> - 2020-01-04 09:01 +0000
                                                    Re: Union type punning in C++ Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-04 10:02 +0100
                                                      Re: Union type punning in C++ Melzzzzz <Melzzzzz@zzzzz.com> - 2020-01-04 09:19 +0000
                                                        Re: Union type punning in C++ Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-04 10:38 +0100
                              Re: Union type punning in C++ David Brown <david.brown@hesbynett.no> - 2020-01-04 12:45 +0100
                                Re: Union type punning in C++ Melzzzzz <Melzzzzz@zzzzz.com> - 2020-01-04 12:00 +0000
                                  Re: Union type punning in C++ Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-04 13:03 +0100
                                Re: Union type punning in C++ Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-04 14:22 +0100
                        Re: Union type punning in C++ "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-01-03 14:46 -0800
                      Re: Union type punning in C++ Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-01-03 10:32 -0800
                        Re: Union type punning in C++ Öö Tiib <ootiib@hot.ee> - 2020-01-03 10:43 -0800
                          Re: Union type punning in C++ Melzzzzz <Melzzzzz@zzzzz.com> - 2020-01-03 18:55 +0000
              Re: Union type punning in C++ Daniel <danielaparker@gmail.com> - 2020-01-08 09:40 -0800
                Re: Union type punning in C++ Öö Tiib <ootiib@hot.ee> - 2020-01-08 13:23 -0800
                  Re: Union type punning in C++ Daniel <danielaparker@gmail.com> - 2020-01-08 22:41 -0800
                  Re: Union type punning in C++ Daniel <danielaparker@gmail.com> - 2020-01-08 22:52 -0800
                    Re: Union type punning in C++ Öö Tiib <ootiib@hot.ee> - 2020-01-09 12:59 -0800
                      Re: Union type punning in C++ Daniel <danielaparker@gmail.com> - 2020-01-09 13:32 -0800
                      Re: Union type punning in C++ Mister2U@honorific.org - 2020-01-10 12:28 +0000
                        Re: Union type punning in C++ David Brown <david.brown@hesbynett.no> - 2020-01-10 14:02 +0100
                          Re: Union type punning in C++ Mister2U@honorific.org - 2020-01-10 16:21 +0000
                            Re: Union type punning in C++ David Brown <david.brown@hesbynett.no> - 2020-01-10 19:18 +0100
                              Re: Union type punning in C++ Daniel <danielaparker@gmail.com> - 2020-01-10 15:41 -0800
                                Re: Union type punning in C++ David Brown <david.brown@hesbynett.no> - 2020-01-11 12:01 +0100
                                  Re: Union type punning in C++ Daniel <danielaparker@gmail.com> - 2020-01-11 07:05 -0800
                                    Re: Union type punning in C++ David Brown <david.brown@hesbynett.no> - 2020-01-11 23:53 +0100
                                      Re: Union type punning in C++ Mr Flibble <flibble@i42.removethisbit.co.uk> - 2020-01-12 01:41 +0000
                                        Re: Union type punning in C++ Daniel <danielaparker@gmail.com> - 2020-01-11 19:00 -0800
                                          Re: Union type punning in C++ Mr Flibble <flibble@i42.removethisbit.co.uk> - 2020-01-13 17:32 +0000
                              Re: Union type punning in C++ Mister2U@honorific.org - 2020-01-11 11:53 +0000
                                Re: Union type punning in C++ David Brown <david.brown@hesbynett.no> - 2020-01-11 13:20 +0100
                                  Re: Union type punning in C++ Mister2U@honorific.org - 2020-01-11 12:33 +0000
                                    Re: Union type punning in C++ David Brown <david.brown@hesbynett.no> - 2020-01-11 14:28 +0100
                            Re: Union type punning in C++ Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-01-11 10:48 +0000
                        Re: Union type punning in C++ Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-01-11 11:05 +0000
                          Re: Union type punning in C++ Mister2U@honorific.org - 2020-01-11 11:55 +0000
            Re: Union type punning in C++ Bo Persson <bo@bo-persson.se> - 2020-01-02 21:12 +0100
              Re: Union type punning in C++ boltar@nowhere.org - 2020-01-03 14:46 +0000
                Re: Union type punning in C++ Bo Persson <bo@bo-persson.se> - 2020-01-03 20:01 +0100
            Re: Union type punning in C++ David Brown <david.brown@hesbynett.no> - 2020-01-02 23:23 +0100
    Re: Union type punning in C++ peter koch <peter.koch.larsen@gmail.com> - 2020-01-02 12:39 -0800
    Re: Union type punning in C++ "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-01-02 15:39 -0800
      Re: Union type punning in C++ Öö Tiib <ootiib@hot.ee> - 2020-01-02 16:58 -0800
        Re: Union type punning in C++ "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-01-02 17:55 -0800
          Re: Union type punning in C++ Öö Tiib <ootiib@hot.ee> - 2020-01-03 00:40 -0800
            Re: Union type punning in C++ Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2020-01-03 14:04 +0000
              Re: Union type punning in C++ David Brown <david.brown@hesbynett.no> - 2020-01-03 15:24 +0100
                Re: Union type punning in C++ "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-01-03 14:34 -0800
                  Re: Union type punning in C++ Melzzzzz <Melzzzzz@zzzzz.com> - 2020-01-03 22:59 +0000
                    Re: Union type punning in C++ Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2020-01-04 00:11 +0000
                      Re: Union type punning in C++ Melzzzzz <Melzzzzz@zzzzz.com> - 2020-01-04 00:21 +0000
                        Re: Union type punning in C++ Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2020-01-04 00:26 +0000
                          Re: Union type punning in C++ "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-01-04 16:51 -0800
                      Re: Union type punning in C++ "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-01-03 18:47 -0800
                        Re: Union type punning in C++ "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-01-03 19:07 -0800
              Re: Union type punning in C++ Öö Tiib <ootiib@hot.ee> - 2020-01-03 06:47 -0800
                Re: Union type punning in C++ Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2020-01-03 14:57 +0000
                  Re: Union type punning in C++ Öö Tiib <ootiib@hot.ee> - 2020-01-03 07:38 -0800
                    Re: Union type punning in C++ Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2020-01-03 17:06 +0000
                      Re: Union type punning in C++ Öö Tiib <ootiib@hot.ee> - 2020-01-03 11:05 -0800
            Re: Union type punning in C++ "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-01-03 14:38 -0800
              Re: Union type punning in C++ Öö Tiib <ootiib@hot.ee> - 2020-01-03 22:37 -0800
        Re: Union type punning in C++ Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-01-11 07:10 -0800
          Re: Union type punning in C++ Öö Tiib <ootiib@hot.ee> - 2020-01-11 10:23 -0800
            Re: Union type punning in C++ Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-01-13 09:01 -0800
              Re: Union type punning in C++ Öö Tiib <ootiib@hot.ee> - 2020-01-13 11:03 -0800
                Re: Union type punning in C++ James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-01-13 12:48 -0800
                  Re: Union type punning in C++ Öö Tiib <ootiib@hot.ee> - 2020-01-13 14:15 -0800
                    Re: Union type punning in C++ James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-01-13 21:47 -0500
                      Re: Union type punning in C++ Öö Tiib <ootiib@hot.ee> - 2020-01-13 23:44 -0800
                        Re: Union type punning in C++ James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-01-14 08:26 -0500
                Re: Union type punning in C++ Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-01-14 07:30 -0800
                  Re: Union type punning in C++ Öö Tiib <ootiib@hot.ee> - 2020-01-14 09:11 -0800
                    Re: Union type punning in C++ James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-01-14 21:52 -0500
                      Re: Union type punning in C++ Öö Tiib <ootiib@hot.ee> - 2020-01-14 23:15 -0800
                    Re: Union type punning in C++ Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-01-21 02:03 -0800
      Re: Union type punning in C++ Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-01-11 07:28 -0800
        Re: Union type punning in C++ "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-01-11 20:40 -0800
          Re: Union type punning in C++ James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-01-12 20:16 -0500
            Re: Union type punning in C++ "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-01-12 17:28 -0800
              Re: Union type punning in C++ James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-01-12 21:53 -0500
                Re: Union type punning in C++ Manfred <noname@add.invalid> - 2020-01-13 18:50 +0100
                Re: Union type punning in C++ "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-01-13 16:21 -0800
              Re: Union type punning in C++ Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-01-13 08:57 -0800
                Re: Union type punning in C++ "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-01-13 16:27 -0800
                  Re: Union type punning in C++ Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-01-14 07:05 -0800
                    Re: Union type punning in C++ "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-01-14 20:58 -0800
                      Re: Union type punning in C++ Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-01-21 03:47 -0800
              Re: Union type punning in C++ Öö Tiib <ootiib@hot.ee> - 2020-01-15 01:22 -0800
                Re: Union type punning in C++ James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-01-15 08:04 -0500
                  Re: Union type punning in C++ Öö Tiib <ootiib@hot.ee> - 2020-01-15 09:38 -0800
                    Re: Union type punning in C++ James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-01-15 11:01 -0800
          Re: Union type punning in C++ Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-01-13 08:37 -0800
            Re: Union type punning in C++ "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-01-13 16:29 -0800

Page 5 of 7 — ← Prev page 1 2 3 4 [5] 6 7  Next page →


#70984

FromDavid Brown <david.brown@hesbynett.no>
Date2020-01-02 23:23 +0100
Message-ID<qulqhg$l9m$1@dont-email.me>
In reply to#70974
On 02/01/2020 18:22, boltar@nowhere.org wrote:
> On Thu, 2 Jan 2020 04:52:42 -0800 (PST)
> =?UTF-8?B?w5bDtiBUaWli?= <ootiib@hot.ee> wrote:
>> On Thursday, 2 January 2020 13:49:39 UTC+2, Bo Persson  wrote:
>>>
>>> The C++ committee seems unwilling to change the union rules and so
>>> instead adds a bit_cast function to package the memcpy usage with a
>>> slightly better name:
>>>
>>> template< class To, class From >
>>> constexpr To bit_cast(const From& from) noexcept;
>>>
>>> https://en.cppreference.com/w/cpp/numeric/bit_cast
>>
>> Thanks, I didn't somehow even notice that. So we can throw away
>> our own type punning wrappers around memcpy in C++20.
> 
> You realise using a union (or a cast) in theory uses very few CPU cycles to do
> type punning, whereas memcpy has the overhead of a function call + loop. Its
> an extremely inefficient way to do it if you don't need the new type to be in
> a seperate variable.
> 

memcpy of a known small size will be handled inline by most decent 
compilers.  Generally it does not lead to any memory accesses or code 
other than register movement.


#include <string.h>

float swapsign1(float x) {
     union { float f; unsigned int i; } u;
     u.f = x;
     u.i ^= 0x80000000;
     return u.f;
}

float swapsign2(float x) {
     unsigned int i;
     memcpy(&i, &x, sizeof(float));
     i ^= 0x80000000;
     memcpy(&x, &i, sizeof(float));
     return x;
}

These give identical code with gcc.  Both versions are valid in C, but 
only swapsign2 is defined by the C++ standards (though gcc defines the 
behaviour as an extension).

[toc] | [prev] | [next] | [standalone]


#70981

Frompeter koch <peter.koch.larsen@gmail.com>
Date2020-01-02 12:39 -0800
Message-ID<0c062de7-3801-4f1d-9d85-cb53529588a7@googlegroups.com>
In reply to#70942
onsdag den 1. januar 2020 kl. 20.24.19 UTC+1 skrev Mr Flibble:
> Undefined behaviour aside is C++ union type punning an acceptable hack or not, given that it is legal in C?
> If it works on all the compilers I care about does it really matter that it is *currently* classed as undefined behaviour?
> I would say that everyone has "secretly" fucking done it some time or other so the C++ Standards Committee should get there act together and legalize it like it has been in C.
> Can anyone provide a straight answer to this?
> 

It does not work with maximum optimization at least with gcc, and it has not done so for at least six years. I know from bitter personal experience (converting from IBM to IEEE floating point).
Instead, just use std::bit_cast - implement it yourself, it is ten lines of code all included. If you compile with optimizations on, the resulting code has no overhead whatsoever.

[toc] | [prev] | [next] | [standalone]


#70986

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2020-01-02 15:39 -0800
Message-ID<quluvv$10pp$1@gioia.aioe.org>
In reply to#70942
On 1/1/2020 11:24 AM, Mr Flibble wrote:
> Undefined behaviour aside is C++ union type punning an acceptable hack 
> or not, given that it is legal in C?
> If it works on all the compilers I care about does it really matter that 
> it is *currently* classed as undefined behaviour?
> I would say that everyone has "secretly" fucking done it some time or 
> other so the C++ Standards Committee should get there act together and 
> legalize it like it has been in C.
> Can anyone provide a straight answer to this?

Is there some cryptic hidden thing in the standard that just might 
"allow" for the following code to run without ub?
___________________________
#include <iostream>

struct base
{
     int p0;
};

struct object_0
{
     base b;
     int p0;
};

struct object_1
{
     base b;
     int p0;
     int p1;
};

union object
{
     base b;
     object_0 o0;
     object_1 o1;
};


int main() {
     object o;

     o.b.p0 = 41;
     o.o0.b.p0 += 1;
     o.o1.b.p0 += 1;

     // humm
     int r = o.b.p0; // ub...

     std::cout << "r = " << r << "\n";

     return 0;
}
___________________________


Wrt POD and a common base object on each sub-object in a union, well 
does it bite the dust with UB?

[toc] | [prev] | [next] | [standalone]


#70988

FromÖö Tiib <ootiib@hot.ee>
Date2020-01-02 16:58 -0800
Message-ID<490dd5d6-fb8a-482b-a88d-550b8a4defd0@googlegroups.com>
In reply to#70986
On Friday, 3 January 2020 01:39:54 UTC+2, Chris M. Thomasson  wrote:
> On 1/1/2020 11:24 AM, Mr Flibble wrote:
> > Undefined behaviour aside is C++ union type punning an acceptable hack 
> > or not, given that it is legal in C?
> > If it works on all the compilers I care about does it really matter that 
> > it is *currently* classed as undefined behaviour?
> > I would say that everyone has "secretly" fucking done it some time or 
> > other so the C++ Standards Committee should get there act together and 
> > legalize it like it has been in C.
> > Can anyone provide a straight answer to this?
> 
> Is there some cryptic hidden thing in the standard that just might 
> "allow" for the following code to run without ub?

Possibly.

> ___________________________
> #include <iostream>
> 
> struct base
> {
>      int p0;
> };

Standard layout classes have lot of additional guarantees.

> struct object_0
> {
>      base b;
>      int p0;
> };

Standard layout with common initial sequence with base.

> struct object_1
> {
>      base b;
>      int p0;
>      int p1;
> };

Same.

> union object
> {
>      base b;
>      object_0 o0;
>      object_1 o1;
> };

Standard layout union with members that have common initial
sequence.

> int main() {
>      object o;
> 
>      o.b.p0 = 41;

That o.b is therefore active member.

>      o.o0.b.p0 += 1;

It is valid to read from inactive member o0 but I am unsure if
write by += now makes it active. Standard layout aggregate types
have piles of privileges. It is dangerous leg gun in C++.
Some novice can see idiom of usage of such privileges and shoot
off his leg elsewhere.

>      o.o1.b.p0 += 1;

Same here.

> 
>      // humm
>      int r = o.b.p0; // ub...

Reading from common initial sequence of non-active standard layout member
is certainly not undefined behavior.

> 
>      std::cout << "r = " << r << "\n";
> 
>      return 0;
> }
> ___________________________
> 
> 
> Wrt POD and a common base object on each sub-object in a union, well 
> does it bite the dust with UB?

By my understanding it can easily be that it is not undefined behavior
there at all. What is the motivation of writing code like that? Does it
cause issues with some compilers? Very strange code can sometimes find
its way to territory of defects of compilers. Compilers are written
by humans too.


[toc] | [prev] | [next] | [standalone]


#70989

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2020-01-02 17:55 -0800
Message-ID<qum6v6$3aj$1@gioia.aioe.org>
In reply to#70988
On 1/2/2020 4:58 PM, Öö Tiib wrote:
> On Friday, 3 January 2020 01:39:54 UTC+2, Chris M. Thomasson  wrote:
>> On 1/1/2020 11:24 AM, Mr Flibble wrote:
>>> Undefined behaviour aside is C++ union type punning an acceptable hack
>>> or not, given that it is legal in C?
>>> If it works on all the compilers I care about does it really matter that
>>> it is *currently* classed as undefined behaviour?
>>> I would say that everyone has "secretly" fucking done it some time or
>>> other so the C++ Standards Committee should get there act together and
>>> legalize it like it has been in C.
>>> Can anyone provide a straight answer to this?
>>
>> Is there some cryptic hidden thing in the standard that just might
>> "allow" for the following code to run without ub?
> 
> Possibly.
[...]
> By my understanding it can easily be that it is not undefined behavior
> there at all. What is the motivation of writing code like that?

Mainly to boil things down for the example. Also, one can use the base 
object as a, say, sequence counter. The types in the union can be used 
for, an event dispatch.

Say the union object is being represented as an object_1.

object_1* o1 = &o.o1;

o1->b.p0 += 1;
o1->p0 += 1;
o1->p1 += 3;
o1->b.p0 += 1;

Now, we can read the sequence count base now matter if we are working 
with an object_0 or object_1.

int seq = o.b.p0;





  Does it
> cause issues with some compilers? Very strange code can sometimes find
> its way to territory of defects of compilers. Compilers are written
> by humans too.



[toc] | [prev] | [next] | [standalone]


#71005

FromÖö Tiib <ootiib@hot.ee>
Date2020-01-03 00:40 -0800
Message-ID<42c28e06-0493-4d9c-a9f1-21e4318d2fdd@googlegroups.com>
In reply to#70989
On Friday, 3 January 2020 03:56:03 UTC+2, Chris M. Thomasson  wrote:
> On 1/2/2020 4:58 PM, Öö Tiib wrote:
> > On Friday, 3 January 2020 01:39:54 UTC+2, Chris M. Thomasson  wrote:
> >> On 1/1/2020 11:24 AM, Mr Flibble wrote:
> >>> Undefined behaviour aside is C++ union type punning an acceptable hack
> >>> or not, given that it is legal in C?
> >>> If it works on all the compilers I care about does it really matter that
> >>> it is *currently* classed as undefined behaviour?
> >>> I would say that everyone has "secretly" fucking done it some time or
> >>> other so the C++ Standards Committee should get there act together and
> >>> legalize it like it has been in C.
> >>> Can anyone provide a straight answer to this?
> >>
> >> Is there some cryptic hidden thing in the standard that just might
> >> "allow" for the following code to run without ub?
> > 
> > Possibly.
> [...]
> > By my understanding it can easily be that it is not undefined behavior
> > there at all. What is the motivation of writing code like that?
> 
> Mainly to boil things down for the example. Also, one can use the base 
> object as a, say, sequence counter. The types in the union can be used 
> for, an event dispatch.

Hmm. I'm not sure I understand the example problem. What the counter
counts and  why you do not keep it outside of union? 

> Say the union object is being represented as an object_1.
> 
> object_1* o1 = &o.o1;
> 
> o1->b.p0 += 1;
> o1->p0 += 1;
> o1->p1 += 3;

Above looks ill-formed, diagnostic required. 
The p0 and p1 were of class types without operator+=(int) specified.

> o1->b.p0 += 1;
> 
> Now, we can read the sequence count base now matter if we are working 
> with an object_0 or object_1.
> 
> int seq = o.b.p0;

I'm still confused. This is all about some kind of aliases and references
(that likely work fine). But existence of those does simplify nothing for
me. There is apparent desire to have multiple members of union active
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.

[toc] | [prev] | [next] | [standalone]


#71012

FromMr Flibble <flibbleREMOVETHISBIT@i42.co.uk>
Date2020-01-03 14:04 +0000
Message-ID<yRHPF.180536$3sa.135707@fx02.dcg>
In reply to#71005
On 03/01/2020 08:40, Öö Tiib wrote:
> On Friday, 3 January 2020 03:56:03 UTC+2, Chris M. Thomasson  wrote:
>> On 1/2/2020 4:58 PM, Öö Tiib wrote:
>>> On Friday, 3 January 2020 01:39:54 UTC+2, Chris M. Thomasson  wrote:
>>>> On 1/1/2020 11:24 AM, Mr Flibble wrote:
>>>>> Undefined behaviour aside is C++ union type punning an acceptable hack
>>>>> or not, given that it is legal in C?
>>>>> If it works on all the compilers I care about does it really matter that
>>>>> it is *currently* classed as undefined behaviour?
>>>>> I would say that everyone has "secretly" fucking done it some time or
>>>>> other so the C++ Standards Committee should get there act together and
>>>>> legalize it like it has been in C.
>>>>> Can anyone provide a straight answer to this?
>>>>
>>>> Is there some cryptic hidden thing in the standard that just might
>>>> "allow" for the following code to run without ub?
>>>
>>> Possibly.
>> [...]
>>> By my understanding it can easily be that it is not undefined behavior
>>> there at all. What is the motivation of writing code like that?
>>
>> Mainly to boil things down for the example. Also, one can use the base
>> object as a, say, sequence counter. The types in the union can be used
>> for, an event dispatch.
> 
> Hmm. I'm not sure I understand the example problem. What the counter
> counts and  why you do not keep it outside of union?
> 
>> Say the union object is being represented as an object_1.
>>
>> object_1* o1 = &o.o1;
>>
>> o1->b.p0 += 1;
>> o1->p0 += 1;
>> o1->p1 += 3;
> 
> Above looks ill-formed, diagnostic required.
> The p0 and p1 were of class types without operator+=(int) specified.
> 
>> o1->b.p0 += 1;
>>
>> Now, we can read the sequence count base now matter if we are working
>> with an object_0 or object_1.
>>
>> int seq = o.b.p0;
> 
> I'm still confused. This is all about some kind of aliases and references
> (that likely work fine). But existence of those does simplify nothing for
> me. There is apparent desire to have multiple members of union active
> 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."

[toc] | [prev] | [next] | [standalone]


#71014

FromDavid Brown <david.brown@hesbynett.no>
Date2020-01-03 15:24 +0100
Message-ID<quniqj$5mo$1@dont-email.me>
In reply to#71012
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.

[toc] | [prev] | [next] | [standalone]


#71052

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2020-01-03 14:34 -0800
Message-ID<quofhq$cf5$1@gioia.aioe.org>
In reply to#71014
On 1/3/2020 6:24 AM, David Brown wrote:
> 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?

Are you familiar with the GLSL language? Think of something along the 
lines of:
________________
vec2 foo = vec2(1., 2.);
vec3 bar = vec3(foo.xy, 3.);
vec4 foobar = vec4(foo.xy, bar.yz);

foobar = { 1., 2., 2., 3. }
________________


> 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.
> 

Afaict, its going to be rather "tedious" to get it done in C++. Humm...

[toc] | [prev] | [next] | [standalone]


#71056

FromMelzzzzz <Melzzzzz@zzzzz.com>
Date2020-01-03 22:59 +0000
Message-ID<fHPPF.235571$C%6.208755@fx23.am4>
In reply to#71052
On 2020-01-03, Chris M. Thomasson <chris.m.thomasson.1@gmail.com> wrote:
> On 1/3/2020 6:24 AM, David Brown wrote:
>> 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?
>
> Are you familiar with the GLSL language? Think of something along the 
> lines of:
> ________________
> vec2 foo = vec2(1., 2.);
> vec3 bar = vec3(foo.xy, 3.);
> vec4 foobar = vec4(foo.xy, bar.yz);
>
> foobar = { 1., 2., 2., 3. }
> ________________
>
>
>> 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.
>> 
>
> Afaict, its going to be rather "tedious" to get it done in C++. Humm...

Or at all needed...
-- 
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

[toc] | [prev] | [next] | [standalone]


#71058

FromMr Flibble <flibbleREMOVETHISBIT@i42.co.uk>
Date2020-01-04 00:11 +0000
Message-ID<vKQPF.35067$nQ7.6832@fx12.dcg>
In reply to#71056
On 03/01/2020 22:59, Melzzzzz wrote:
> On 2020-01-03, Chris M. Thomasson <chris.m.thomasson.1@gmail.com> wrote:
>> On 1/3/2020 6:24 AM, David Brown wrote:
>>> 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?
>>
>> Are you familiar with the GLSL language? Think of something along the
>> lines of:
>> ________________
>> vec2 foo = vec2(1., 2.);
>> vec3 bar = vec3(foo.xy, 3.);
>> vec4 foobar = vec4(foo.xy, bar.yz);
>>
>> foobar = { 1., 2., 2., 3. }
>> ________________
>>
>>
>>> 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.
>>>
>>
>> Afaict, its going to be rather "tedious" to get it done in C++. Humm...
> 
> Or at all needed...

It is needed to make client-site code as simple/clean/elegant as possible:

clean:

v.x = 42; f(v.x)

not-clean:

v.set_x(42); f(v.x());

/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."

[toc] | [prev] | [next] | [standalone]


#71061

FromMelzzzzz <Melzzzzz@zzzzz.com>
Date2020-01-04 00:21 +0000
Message-ID<TTQPF.70884$mq3.21341@fx20.am4>
In reply to#71058
On 2020-01-04, Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> wrote:
> On 03/01/2020 22:59, Melzzzzz wrote:
>> On 2020-01-03, Chris M. Thomasson <chris.m.thomasson.1@gmail.com> wrote:
>>> On 1/3/2020 6:24 AM, David Brown wrote:
>>>> 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?
>>>
>>> Are you familiar with the GLSL language? Think of something along the
>>> lines of:
>>> ________________
>>> vec2 foo = vec2(1., 2.);
>>> vec3 bar = vec3(foo.xy, 3.);
>>> vec4 foobar = vec4(foo.xy, bar.yz);
>>>
>>> foobar = { 1., 2., 2., 3. }
>>> ________________
>>>
>>>
>>>> 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.
>>>>
>>>
>>> Afaict, its going to be rather "tedious" to get it done in C++. Humm...
>> 
>> Or at all needed...
>
> It is needed to make client-site code as simple/clean/elegant as possible:
>
> clean:
>
> v.x = 42; f(v.x)
>
> not-clean:
>
> v.set_x(42); f(v.x());

It's just syntax. Preference.
Object vs functional style.
We all know that is advisable not to touch member variables directly.
When that changed?

>
> /Flibble
>


-- 
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

[toc] | [prev] | [next] | [standalone]


#71064

FromMr Flibble <flibbleREMOVETHISBIT@i42.co.uk>
Date2020-01-04 00:26 +0000
Message-ID<zYQPF.58634$QD3.42185@fx08.dcg>
In reply to#71061
On 04/01/2020 00:21, Melzzzzz wrote:
> On 2020-01-04, Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> wrote:
>> On 03/01/2020 22:59, Melzzzzz wrote:
>>> On 2020-01-03, Chris M. Thomasson <chris.m.thomasson.1@gmail.com> wrote:
>>>> On 1/3/2020 6:24 AM, David Brown wrote:
>>>>> 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?
>>>>
>>>> Are you familiar with the GLSL language? Think of something along the
>>>> lines of:
>>>> ________________
>>>> vec2 foo = vec2(1., 2.);
>>>> vec3 bar = vec3(foo.xy, 3.);
>>>> vec4 foobar = vec4(foo.xy, bar.yz);
>>>>
>>>> foobar = { 1., 2., 2., 3. }
>>>> ________________
>>>>
>>>>
>>>>> 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.
>>>>>
>>>>
>>>> Afaict, its going to be rather "tedious" to get it done in C++. Humm...
>>>
>>> Or at all needed...
>>
>> It is needed to make client-site code as simple/clean/elegant as possible:
>>
>> clean:
>>
>> v.x = 42; f(v.x)
>>
>> not-clean:
>>
>> v.set_x(42); f(v.x());
> 
> It's just syntax. Preference.
> Object vs functional style.
> We all know that is advisable not to touch member variables directly.
> When that changed?

There isn't a class invariant so it is no different to using a "struct" (yes, I know there isn't much difference between "struct" and "class" in C++).

/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."

[toc] | [prev] | [next] | [standalone]


#71123

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2020-01-04 16:51 -0800
Message-ID<qurbum$13k2$1@gioia.aioe.org>
In reply to#71064
On 1/3/2020 4:26 PM, Mr Flibble wrote:
> On 04/01/2020 00:21, Melzzzzz wrote:
>> On 2020-01-04, Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> wrote:
>>> On 03/01/2020 22:59, Melzzzzz wrote:
>>>> On 2020-01-03, Chris M. Thomasson <chris.m.thomasson.1@gmail.com> 
>>>> wrote:
>>>>> On 1/3/2020 6:24 AM, David Brown wrote:
>>>>>> 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?
>>>>>
>>>>> Are you familiar with the GLSL language? Think of something along the
>>>>> lines of:
>>>>> ________________
>>>>> vec2 foo = vec2(1., 2.);
>>>>> vec3 bar = vec3(foo.xy, 3.);
>>>>> vec4 foobar = vec4(foo.xy, bar.yz);
>>>>>
>>>>> foobar = { 1., 2., 2., 3. }
>>>>> ________________
>>>>>
>>>>>
>>>>>> 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.
>>>>>>
>>>>>
>>>>> Afaict, its going to be rather "tedious" to get it done in C++. 
>>>>> Humm...
>>>>
>>>> Or at all needed...
>>>
>>> It is needed to make client-site code as simple/clean/elegant as 
>>> possible:
>>>
>>> clean:
>>>
>>> v.x = 42; f(v.x)
>>>
>>> not-clean:
>>>
>>> v.set_x(42); f(v.x());
>>
>> It's just syntax. Preference.
>> Object vs functional style.
>> We all know that is advisable not to touch member variables directly.
>> When that changed?
> 
> There isn't a class invariant so it is no different to using a "struct" 
> (yes, I know there isn't much difference between "struct" and "class" in 
> C++).

Side note, I actually saw a person claim that there was no difference, 
then complained that everything is private.

[toc] | [prev] | [next] | [standalone]


#71075

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2020-01-03 18:47 -0800
Message-ID<quoubs$c4c$1@gioia.aioe.org>
In reply to#71058
On 1/3/2020 4:11 PM, Mr Flibble wrote:
> On 03/01/2020 22:59, Melzzzzz wrote:
>> On 2020-01-03, Chris M. Thomasson <chris.m.thomasson.1@gmail.com> wrote:
>>> On 1/3/2020 6:24 AM, David Brown wrote:
>>>> 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?
>>>
>>> Are you familiar with the GLSL language? Think of something along the
>>> lines of:
>>> ________________
>>> vec2 foo = vec2(1., 2.);
>>> vec3 bar = vec3(foo.xy, 3.);
>>> vec4 foobar = vec4(foo.xy, bar.yz);
>>>
>>> foobar = { 1., 2., 2., 3. }
>>> ________________
>>>
>>>
>>>> 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.
>>>>
>>>
>>> Afaict, its going to be rather "tedious" to get it done in C++. Humm...
>>
>> Or at all needed...
> 
> It is needed to make client-site code as simple/clean/elegant as possible:

The cleaner the better, wrt trying to port GLSL code to its C++ "equal". 
It can be done, and a nice std swizzle would be great. Now, going from 
C++ to GLSL is easy, in a sense. The other way around, sometimes, get 
ready for fun! ;^o


> clean:
> 
> v.x = 42; f(v.x)
> 
> not-clean:
> 
> v.set_x(42); f(v.x());

I see, indeed.

[toc] | [prev] | [next] | [standalone]


#71076

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2020-01-03 19:07 -0800
Message-ID<quovhq$ggj$1@gioia.aioe.org>
In reply to#71075
On 1/3/2020 6:47 PM, Chris M. Thomasson wrote:
> On 1/3/2020 4:11 PM, Mr Flibble wrote:
>> On 03/01/2020 22:59, Melzzzzz wrote:
>>> On 2020-01-03, Chris M. Thomasson <chris.m.thomasson.1@gmail.com> wrote:
>>>> On 1/3/2020 6:24 AM, David Brown wrote:
>>>>> 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?
>>>>
>>>> Are you familiar with the GLSL language? Think of something along the
>>>> lines of:
>>>> ________________
>>>> vec2 foo = vec2(1., 2.);
>>>> vec3 bar = vec3(foo.xy, 3.);
>>>> vec4 foobar = vec4(foo.xy, bar.yz);
>>>>
>>>> foobar = { 1., 2., 2., 3. }
>>>> ________________
>>>>
>>>>
>>>>> 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.
>>>>>
>>>>
>>>> Afaict, its going to be rather "tedious" to get it done in C++. Humm...
>>>
>>> Or at all needed...
>>
>> It is needed to make client-site code as simple/clean/elegant as 
>> possible:
> 
> The cleaner the better, wrt trying to port GLSL code to its C++ "equal". 
> It can be done, and a nice std swizzle would be great. Now, going from 
> C++ to GLSL is easy, in a sense. The other way around, sometimes, get 
> ready for fun! ;^o
[...]

I have to admit that this is sort of "selfish". Imvvho, I would not 
object to C++ supporting some sort of GLSL like swizzle.

[toc] | [prev] | [next] | [standalone]


#71016

FromÖö Tiib <ootiib@hot.ee>
Date2020-01-03 06:47 -0800
Message-ID<658bc900-0a9c-4430-bcf6-20e227d2c31f@googlegroups.com>
In reply to#71012
On Friday, 3 January 2020 16:04:31 UTC+2, Mr Flibble  wrote:
> On 03/01/2020 08:40, Öö Tiib wrote:
> > On Friday, 3 January 2020 03:56:03 UTC+2, Chris M. Thomasson  wrote:
> >> On 1/2/2020 4:58 PM, Öö Tiib wrote:
> >>> On Friday, 3 January 2020 01:39:54 UTC+2, Chris M. Thomasson  wrote:
> >>>> On 1/1/2020 11:24 AM, Mr Flibble wrote:
> >>>>> Undefined behaviour aside is C++ union type punning an acceptable hack
> >>>>> or not, given that it is legal in C?
> >>>>> If it works on all the compilers I care about does it really matter that
> >>>>> it is *currently* classed as undefined behaviour?
> >>>>> I would say that everyone has "secretly" fucking done it some time or
> >>>>> other so the C++ Standards Committee should get there act together and
> >>>>> legalize it like it has been in C.
> >>>>> Can anyone provide a straight answer to this?
> >>>>
> >>>> Is there some cryptic hidden thing in the standard that just might
> >>>> "allow" for the following code to run without ub?
> >>>
> >>> Possibly.
> >> [...]
> >>> By my understanding it can easily be that it is not undefined behavior
> >>> there at all. What is the motivation of writing code like that?
> >>
> >> Mainly to boil things down for the example. Also, one can use the base
> >> object as a, say, sequence counter. The types in the union can be used
> >> for, an event dispatch.
> > 
> > Hmm. I'm not sure I understand the example problem. What the counter
> > counts and  why you do not keep it outside of union?
> > 
> >> Say the union object is being represented as an object_1.
> >>
> >> object_1* o1 = &o.o1;
> >>
> >> o1->b.p0 += 1;
> >> o1->p0 += 1;
> >> o1->p1 += 3;
> > 
> > Above looks ill-formed, diagnostic required.
> > The p0 and p1 were of class types without operator+=(int) specified.
> > 
> >> o1->b.p0 += 1;
> >>
> >> Now, we can read the sequence count base now matter if we are working
> >> with an object_0 or object_1.
> >>
> >> int seq = o.b.p0;
> > 
> > I'm still confused. This is all about some kind of aliases and references
> > (that likely work fine). But existence of those does simplify nothing for
> > me. There is apparent desire to have multiple members of union active
> > 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.

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.

[toc] | [prev] | [next] | [standalone]


#71020

FromMr Flibble <flibbleREMOVETHISBIT@i42.co.uk>
Date2020-01-03 14:57 +0000
Message-ID<yDIPF.266826$5I8.97069@fx04.dcg>
In reply to#71016
On 03/01/2020 14:47, Öö Tiib wrote:
> On Friday, 3 January 2020 16:04:31 UTC+2, Mr Flibble  wrote:
>> On 03/01/2020 08:40, Öö Tiib wrote:
>>> On Friday, 3 January 2020 03:56:03 UTC+2, Chris M. Thomasson  wrote:
>>>> On 1/2/2020 4:58 PM, Öö Tiib wrote:
>>>>> On Friday, 3 January 2020 01:39:54 UTC+2, Chris M. Thomasson  wrote:
>>>>>> On 1/1/2020 11:24 AM, Mr Flibble wrote:
>>>>>>> Undefined behaviour aside is C++ union type punning an acceptable hack
>>>>>>> or not, given that it is legal in C?
>>>>>>> If it works on all the compilers I care about does it really matter that
>>>>>>> it is *currently* classed as undefined behaviour?
>>>>>>> I would say that everyone has "secretly" fucking done it some time or
>>>>>>> other so the C++ Standards Committee should get there act together and
>>>>>>> legalize it like it has been in C.
>>>>>>> Can anyone provide a straight answer to this?
>>>>>>
>>>>>> Is there some cryptic hidden thing in the standard that just might
>>>>>> "allow" for the following code to run without ub?
>>>>>
>>>>> Possibly.
>>>> [...]
>>>>> By my understanding it can easily be that it is not undefined behavior
>>>>> there at all. What is the motivation of writing code like that?
>>>>
>>>> Mainly to boil things down for the example. Also, one can use the base
>>>> object as a, say, sequence counter. The types in the union can be used
>>>> for, an event dispatch.
>>>
>>> Hmm. I'm not sure I understand the example problem. What the counter
>>> counts and  why you do not keep it outside of union?
>>>
>>>> Say the union object is being represented as an object_1.
>>>>
>>>> object_1* o1 = &o.o1;
>>>>
>>>> o1->b.p0 += 1;
>>>> o1->p0 += 1;
>>>> o1->p1 += 3;
>>>
>>> Above looks ill-formed, diagnostic required.
>>> The p0 and p1 were of class types without operator+=(int) specified.
>>>
>>>> o1->b.p0 += 1;
>>>>
>>>> Now, we can read the sequence count base now matter if we are working
>>>> with an object_0 or object_1.
>>>>
>>>> int seq = o.b.p0;
>>>
>>> I'm still confused. This is all about some kind of aliases and references
>>> (that likely work fine). But existence of those does simplify nothing for
>>> me. There is apparent desire to have multiple members of union active
>>> 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.
> 
> 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.

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."

[toc] | [prev] | [next] | [standalone]


#71026

FromÖö Tiib <ootiib@hot.ee>
Date2020-01-03 07:38 -0800
Message-ID<d551d721-a949-4d87-b550-4705b33374ec@googlegroups.com>
In reply to#71020
On Friday, 3 January 2020 16:57:50 UTC+2, Mr Flibble  wrote:
> On 03/01/2020 14:47, Öö Tiib wrote:
> > On Friday, 3 January 2020 16:04:31 UTC+2, Mr Flibble  wrote:
> >> On 03/01/2020 08:40, Öö Tiib wrote:
> >>> On Friday, 3 January 2020 03:56:03 UTC+2, Chris M. Thomasson  wrote:
> >>>> On 1/2/2020 4:58 PM, Öö Tiib wrote:
> >>>>> On Friday, 3 January 2020 01:39:54 UTC+2, Chris M. Thomasson  wrote:
> >>>>>> On 1/1/2020 11:24 AM, Mr Flibble wrote:
> >>>>>>> Undefined behaviour aside is C++ union type punning an acceptable hack
> >>>>>>> or not, given that it is legal in C?
> >>>>>>> If it works on all the compilers I care about does it really matter that
> >>>>>>> it is *currently* classed as undefined behaviour?
> >>>>>>> I would say that everyone has "secretly" fucking done it some time or
> >>>>>>> other so the C++ Standards Committee should get there act together and
> >>>>>>> legalize it like it has been in C.
> >>>>>>> Can anyone provide a straight answer to this?
> >>>>>>
> >>>>>> Is there some cryptic hidden thing in the standard that just might
> >>>>>> "allow" for the following code to run without ub?
> >>>>>
> >>>>> Possibly.
> >>>> [...]
> >>>>> By my understanding it can easily be that it is not undefined behavior
> >>>>> there at all. What is the motivation of writing code like that?
> >>>>
> >>>> Mainly to boil things down for the example. Also, one can use the base
> >>>> object as a, say, sequence counter. The types in the union can be used
> >>>> for, an event dispatch.
> >>>
> >>> Hmm. I'm not sure I understand the example problem. What the counter
> >>> counts and  why you do not keep it outside of union?
> >>>
> >>>> Say the union object is being represented as an object_1.
> >>>>
> >>>> object_1* o1 = &o.o1;
> >>>>
> >>>> o1->b.p0 += 1;
> >>>> o1->p0 += 1;
> >>>> o1->p1 += 3;
> >>>
> >>> Above looks ill-formed, diagnostic required.
> >>> The p0 and p1 were of class types without operator+=(int) specified.
> >>>
> >>>> o1->b.p0 += 1;
> >>>>
> >>>> Now, we can read the sequence count base now matter if we are working
> >>>> with an object_0 or object_1.
> >>>>
> >>>> int seq = o.b.p0;
> >>>
> >>> I'm still confused. This is all about some kind of aliases and references
> >>> (that likely work fine). But existence of those does simplify nothing for
> >>> me. There is apparent desire to have multiple members of union active
> >>> 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.
> > 
> > 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.
> 
> No that won't help me with my swizzle class.

Why? Can it be rewritten in a way that will help with it or are
there some fundamental problems? 

[toc] | [prev] | [next] | [standalone]


#71037

FromMr Flibble <flibbleREMOVETHISBIT@i42.co.uk>
Date2020-01-03 17:06 +0000
Message-ID<awKPF.84012$pk7.69549@fx05.dcg>
In reply to#71026
On 03/01/2020 15:38, Öö Tiib wrote:
> On Friday, 3 January 2020 16:57:50 UTC+2, Mr Flibble  wrote:
>> On 03/01/2020 14:47, Öö Tiib wrote:
>>> On Friday, 3 January 2020 16:04:31 UTC+2, Mr Flibble  wrote:
>>>> On 03/01/2020 08:40, Öö Tiib wrote:
>>>>> On Friday, 3 January 2020 03:56:03 UTC+2, Chris M. Thomasson  wrote:
>>>>>> On 1/2/2020 4:58 PM, Öö Tiib wrote:
>>>>>>> On Friday, 3 January 2020 01:39:54 UTC+2, Chris M. Thomasson  wrote:
>>>>>>>> On 1/1/2020 11:24 AM, Mr Flibble wrote:
>>>>>>>>> Undefined behaviour aside is C++ union type punning an acceptable hack
>>>>>>>>> or not, given that it is legal in C?
>>>>>>>>> If it works on all the compilers I care about does it really matter that
>>>>>>>>> it is *currently* classed as undefined behaviour?
>>>>>>>>> I would say that everyone has "secretly" fucking done it some time or
>>>>>>>>> other so the C++ Standards Committee should get there act together and
>>>>>>>>> legalize it like it has been in C.
>>>>>>>>> Can anyone provide a straight answer to this?
>>>>>>>>
>>>>>>>> Is there some cryptic hidden thing in the standard that just might
>>>>>>>> "allow" for the following code to run without ub?
>>>>>>>
>>>>>>> Possibly.
>>>>>> [...]
>>>>>>> By my understanding it can easily be that it is not undefined behavior
>>>>>>> there at all. What is the motivation of writing code like that?
>>>>>>
>>>>>> Mainly to boil things down for the example. Also, one can use the base
>>>>>> object as a, say, sequence counter. The types in the union can be used
>>>>>> for, an event dispatch.
>>>>>
>>>>> Hmm. I'm not sure I understand the example problem. What the counter
>>>>> counts and  why you do not keep it outside of union?
>>>>>
>>>>>> Say the union object is being represented as an object_1.
>>>>>>
>>>>>> object_1* o1 = &o.o1;
>>>>>>
>>>>>> o1->b.p0 += 1;
>>>>>> o1->p0 += 1;
>>>>>> o1->p1 += 3;
>>>>>
>>>>> Above looks ill-formed, diagnostic required.
>>>>> The p0 and p1 were of class types without operator+=(int) specified.
>>>>>
>>>>>> o1->b.p0 += 1;
>>>>>>
>>>>>> Now, we can read the sequence count base now matter if we are working
>>>>>> with an object_0 or object_1.
>>>>>>
>>>>>> int seq = o.b.p0;
>>>>>
>>>>> I'm still confused. This is all about some kind of aliases and references
>>>>> (that likely work fine). But existence of those does simplify nothing for
>>>>> me. There is apparent desire to have multiple members of union active
>>>>> 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.
>>>
>>> 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.
>>
>> No that won't help me with my swizzle class.
> 
> Why? Can it be rewritten in a way that will help with it or are
> there some fundamental problems?

You can find my swizzle base class here:

https://github.com/i42output/neoGFX/blob/master/include/neogfx/core/swizzle_array.hpp

used by my swizzle class here:

https://github.com/i42output/neoGFX/blob/master/include/neogfx/core/swizzle.hpp

used by my vector class here:

https://github.com/i42output/neoGFX/blob/master/include/neogfx/core/numerical.hpp

/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."

[toc] | [prev] | [next] | [standalone]


Page 5 of 7 — ← Prev page 1 2 3 4 [5] 6 7  Next page →

Back to top | Article view | comp.lang.c++


csiph-web