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


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

Re: Is there a way to determine endianess in a constexpr-function ?

Subject Re: Is there a way to determine endianess in a constexpr-function ?
Newsgroups comp.lang.c++
References <quiidc$qa1$1@news.albasani.net> <1yyPF.127450$QU2.87340@fx01.iad> <qumqk4$okt$1@news.albasani.net> <qumrb1$n08$1@news.albasani.net>
From Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo>
Message-ID <0QeQF.95983$Cb2.42884@fx44.iad> (permalink)
Date 2020-01-05 00:51 -0500

Show all headers | View raw


Bonita Montero wrote:
> Am 03.01.2020 um 08:31 schrieb Bonita Montero:
>>>> Is there a way to determine endianess in a constexpr-function
>>>> which might return true for little endian and false for big
>>>> endian? I guess not, but maybe I din't consider a trick here.
>>> Why not, it's actually not difficult.
>>
>>> Below is a simple example (actually returns true for big endian)
>>> but I think more complex checks can be built upon same principle.
>>> #include <cstdint>
>>> #include <iostream>
>>> using namespace std;
>>> enum ProbeEnum : int16_t { ProbeValue = 0x0100 };
>>> constexpr union {
>>>    ProbeEnum p;
>>>    char ca[2];
>>> } ProbeVal{ProbeValue};
>>> constexpr
>>> bool
>>> IsBigEndian()
>>> {
>>>    return !!ProbeVal.ca[0];
>>> }
>>> int
>>> main(int, char*[])
>>> {
>>>    cout << "IsBigEndian=" << IsBigEndian() << endl;
>>>
>>>
>>>    return 0;
>>> }
>>
>> IsBigEndian isn't guaranteed compile-time evaluated.
> 
> if i do: "if constexpr( IsBigEndian() )" I'll get:
> "error C2131: expression did not evaluate to a constant"
Right. If I try to do

  constexpr bool ibe = IsBigEndian();

I receive more explicit error message from gcc:

 error: accessing ‘Endian::ca’ member instead of initialized ‘Endian::p’ member
in constant expression

So the Big Brother is watching.. Then the answer to your question is probably
"not possible until C++20". With C++20 you should be able to write a similar
code using bit_cast which is constexpr as mentioned by Bo Persson on another thread.

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


Thread

Is there a way to determine endianess in a constexpr-function ? Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-01 17:46 +0100
  Re: Is there a way to determine endianess in a constexpr-function ? Bo Persson <bo@bo-persson.se> - 2020-01-01 18:49 +0100
    Re: Is there a way to determine endianess in a constexpr-function ? Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-01 18:54 +0100
      Re: Is there a way to determine endianess in a constexpr-function ? David Brown <david.brown@hesbynett.no> - 2020-01-01 23:21 +0100
        Re: Is there a way to determine endianess in a constexpr-function ? Robert Wessel <robertwessel2@yahoo.com> - 2020-01-01 18:26 -0600
        Re: Is there a way to determine endianess in a constexpr-function ? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-01-02 00:47 +0000
  Re: Is there a way to determine endianess in a constexpr-function ? Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2020-01-02 22:28 -0500
    Re: Is there a way to determine endianess in a constexpr-function ? Siri Cruise <chine.bleu@yahoo.com> - 2020-01-02 19:51 -0800
    Re: Is there a way to determine endianess in a constexpr-function ? Öö Tiib <ootiib@hot.ee> - 2020-01-02 23:14 -0800
    Re: Is there a way to determine endianess in a constexpr-function ? Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-03 08:31 +0100
      Re: Is there a way to determine endianess in a constexpr-function ? Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-03 08:43 +0100
        Re: Is there a way to determine endianess in a constexpr-function ? Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2020-01-05 00:51 -0500
          Re: Is there a way to determine endianess in a constexpr-function ? Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-05 07:43 +0100
            Re: Is there a way to determine endianess in a constexpr-function ? Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-05 08:31 +0100
              Re: Is there a way to determine endianess in a constexpr-function ? Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-05 08:34 +0100
              Re: Is there a way to determine endianess in a constexpr-function ? Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-05 08:38 +0100

csiph-web