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


Groups > comp.lang.c++ > #86819

Re: how to specify an 8 byte bool ?

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c++
Subject Re: how to specify an 8 byte bool ?
Date 2022-10-06 14:14 +0200
Organization A noiseless patient Spider
Message-ID <thmgqu$3br22$1@dont-email.me> (permalink)
References <thinbh$2pm70$1@dont-email.me> <87fsg3rn6s.fsf@nosuchdomain.example.com> <thiu1f$2t2ra$1@dont-email.me> <thksdo$14l$1@gioia.aioe.org> <31f4c07e-1ec1-4cd8-a095-bd58f6af64e9n@googlegroups.com>

Show all headers | View raw


On 06/10/2022 03:45, daniel...@gmail.com wrote:
> On Wednesday, October 5, 2022 at 5:20:09 PM UTC-4, F.Zwarts wrote:
>> Op 05.okt..2022 om 05:35 schreef Lynn McGuire:
> 
>>> I've got a double array, integer*8 array, logical*8 array, and
>>> character*8 array equivalenced in my Fortran code that I am converting
>>> to C++.  I want to have the same capability in my C++ code until I get
>>> down the road and figure out what I want to do with this code, if anything.
>> The Fortran EQUIVALENT is in my mind roughly equivalent to a C++ union.
> 
>> So, I would probably go for an array of unions. Where each union is a
>> union of a double, integer, bool and char[8]. The bool would then
>> automatically use the space of the largest type in the union.
> 
> I think it would be undefined behaviour to use a C++ union for a FORTRAN
> EQUIVALENCE statement. C++ (unlike C) does not permit type pruning.
> 

I think "type pruning" sounds more fun than mere "type punning".  It 
sounds like a way to clear up a mess of too many types, leaving only the 
pretty ones :-)

I guess the answer here is a class which has a single storage unit 
(uint64_t) with constructors and conversion operators to the different 
types, using memcpy() to make it all legal.  A decent compiler will 
optimise away the memcpy().

Alternatively, if you can limit yourself to gcc, clang, icc, and other 
compilers that copy gcc's attributes, the "may_alias" attribute on a 
type lets you use it for type punning (it gives the type the superpowers 
of a character type for accesses).

#include <cstdint>
#include <cstring>
#include <array>

class DIBC {
private :
     uint64_t storage;
     template <class T> constexpr uint64_t toUint64_t(const T x) {
         static_assert(sizeof(x) == 8);
         uint64_t y;
         std::memcpy(&y, &x, 8);
         return y;
     }
     template <class T> constexpr T fromUint64_t(uint64_t x) {
         static_assert(sizeof(T) == 8);
         T y;
         std::memcpy(&y, &x, 8);
         return y;
     }
public :
     using char8 = std::array<char, 8>;
     constexpr DIBC() : storage(0) {}
     constexpr DIBC(uint64_t x) : storage(x) {}
     constexpr DIBC(int64_t x) : storage(x) {}
     constexpr DIBC(bool x) : storage(x) {}
     constexpr DIBC(char8 x) : storage(toUint64_t(x)) {}

     constexpr operator uint64_t() const { return storage; }
     constexpr operator int64_t() const { return storage; }
     constexpr operator bool() const { return storage; }
     constexpr operator char8 const () { return 
fromUint64_t<char8>(storage); }
};




Back to comp.lang.c++ | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

how to specify an 8 byte bool ? Lynn McGuire <lynnmcguire5@gmail.com> - 2022-10-04 20:41 -0500
  Re: how to specify an 8 byte bool ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-10-04 19:58 -0700
    Re: how to specify an 8 byte bool ? Lynn McGuire <lynnmcguire5@gmail.com> - 2022-10-04 22:35 -0500
      Re: how to specify an 8 byte bool ? Christian Gollwitzer <auriocus@gmx.de> - 2022-10-05 07:38 +0200
        Re: how to specify an 8 byte bool ? Lynn McGuire <lynnmcguire5@gmail.com> - 2022-10-05 14:02 -0500
        Re: how to specify an 8 byte bool ? Lynn McGuire <lynnmcguire5@gmail.com> - 2022-10-05 14:03 -0500
      Re: how to specify an 8 byte bool ? David Brown <david.brown@hesbynett.no> - 2022-10-05 09:38 +0200
        Re: how to specify an 8 byte bool ? Lynn McGuire <lynnmcguire5@gmail.com> - 2022-10-05 14:03 -0500
      Re: how to specify an 8 byte bool ? "Fred. Zwarts" <F.Zwarts@KVI.nl> - 2022-10-05 23:19 +0200
        Re: how to specify an 8 byte bool ? "daniel...@gmail.com" <danielaparker@gmail.com> - 2022-10-05 18:45 -0700
          Re: how to specify an 8 byte bool ? Lynn McGuire <lynnmcguire5@gmail.com> - 2022-10-05 21:05 -0500
            Re: how to specify an 8 byte bool ? "daniel...@gmail.com" <danielaparker@gmail.com> - 2022-10-05 19:07 -0700
          Re: how to specify an 8 byte bool ? "Fred. Zwarts" <F.Zwarts@KVI.nl> - 2022-10-06 10:39 +0200
          Re: how to specify an 8 byte bool ? David Brown <david.brown@hesbynett.no> - 2022-10-06 14:14 +0200
          Re: how to specify an 8 byte bool ? scott@slp53.sl.home (Scott Lurndal) - 2022-10-06 14:03 +0000
  Re: how to specify an 8 byte bool ? Lynn McGuire <lynnmcguire5@gmail.com> - 2022-10-05 14:10 -0500
    Re: how to specify an 8 byte bool ? scott@slp53.sl.home (Scott Lurndal) - 2022-10-05 20:34 +0000
      Re: how to specify an 8 byte bool ? Lynn McGuire <lynnmcguire5@gmail.com> - 2022-10-05 16:18 -0500
        Re: how to specify an 8 byte bool ? scott@slp53.sl.home (Scott Lurndal) - 2022-10-05 22:25 +0000
          Re: how to specify an 8 byte bool ? scott@slp53.sl.home (Scott Lurndal) - 2022-10-05 22:27 +0000
            Re: how to specify an 8 byte bool ? Lynn McGuire <lynnmcguire5@gmail.com> - 2022-10-05 19:16 -0500
    Re: how to specify an 8 byte bool ? David Brown <david.brown@hesbynett.no> - 2022-10-06 14:16 +0200
      Re: how to specify an 8 byte bool ? Lynn McGuire <lynnmcguire5@gmail.com> - 2022-10-06 15:07 -0500
        Re: how to specify an 8 byte bool ? David Brown <david.brown@hesbynett.no> - 2022-10-07 09:07 +0200
          Re: how to specify an 8 byte bool ? Lynn McGuire <lynnmcguire5@gmail.com> - 2022-10-07 14:23 -0500
  Re: how to specify an 8 byte bool ? Bonita Montero <Bonita.Montero@gmail.com> - 2022-10-10 18:04 +0200

csiph-web