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


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

Re: String integer id

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c++
Subject Re: String integer id
Date 2016-02-23 22:29 +0100
Organization A noiseless patient Spider
Message-ID <naiipd$kkh$1@dont-email.me> (permalink)
References (5 earlier) <nagaf3$gol$2@jstuckle.eternal-september.org> <nagcau$n0j$1@dont-email.me> <nagcrj$nvc$1@jstuckle.eternal-september.org> <nah4ak$hl6$1@dont-email.me> <7LednbJiLbgVGlHLnZ2dnUU78KGdnZ2d@giganews.com>

Show all headers | View raw


On 23/02/16 17:23, Paavo Helde wrote:
> On 23.02.2016 10:16, David Brown wrote:
>> On 23/02/16 02:35, Jerry Stuckle wrote:
>>> On 2/22/2016 8:26 PM, Alf P. Steinbach wrote:
>>>> On 23.02.2016 01:54, Jerry Stuckle wrote:
>>>>> On 2/22/2016 6:16 PM, Öö Tiib wrote:
>>>>>>
>>>>>> There are no standard trick neither in C nor in C++ to determine
>>>>>> endianness compile time. Any type punning is forbidden in 'constexpr'
>>>>>> functions. For practical purposes Boost header file endian.hpp covers
>>>>>> endianness for large amount of platforms.
>>>>>>
>>>>>
>>>>> Wrong again.
>>>>
>>>> Öö Tiib is correct. There are no known tricks, and absolutely no
>>>> standard or well known ones. The [1] C FAQ mentions a possible trick,
>>>>
>>> Wrong again.  There are ways, using unions.  But I'm not going to get
>>> into an argument with a troll.
>>>
>>
>> I would like to know of a reliable, standard way of identifying
>> endianness at compile time.  Bonus points if it works in C too.  I don't
>> believe such a method exists, but you state it with such confidence.
>> There are plenty of ways of detecting endianness at run-time, and for
>> many of them compilers can typically optimise the check and
>> pre-calculate it:
>>
>> #define I_AM_LITTLE (((union { unsigned x; unsigned char c; }){1}).c)
>>
>> But such checks are not constant expressions - you cannot use the result
>> in things like static initialisers, constexpr expressions, static
>> asserts, #if directives, etc.
>
> Are these constexpr limitations (like banning reinterpret_cast) there
> because of cross-compilation? Is it that if reinterpret_cast were
> allowed in constexpr, the compiler would need to emulate the target
> processor, whereas by current rules it only has to calculate the correct
> portable value?
>

I don't know.  I suspect it is more of a historical limitation - there 
are many things that a modern compiler can calculate at compile time, 
which early compilers could not do.  If something is legal as a constant 
expression, then any compiler, even an unsophisticated and unoptimising 
one, has to figure it out at compile time - otherwise you would end up 
with code that was legal on one compiler and illegal on another one.  I 
suppose the standards /could/ say "whether an expression using a 
reinterpret_cast is a constant expression or not is implementation 
dependent", but I think that would add a good deal of complications to 
the language.

But Jerry is going to show us a compile-time constant expression to 
detect endianness using pure standard C++, so that will settle the matter.

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


Thread

String integer id malcolm.mclean5@btinternet.com - 2016-02-22 06:59 -0800
  Re: String integer id "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2016-02-22 17:30 +0100
    Re: String integer id malcolm.mclean5@btinternet.com - 2016-02-22 09:04 -0800
      Re: String integer id "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2016-02-22 19:03 +0100
        Re: String integer id malcolm.mclean5@btinternet.com - 2016-02-22 13:22 -0800
      Re: String integer id Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-02-22 23:02 +0000
        Re: String integer id Öö Tiib <ootiib@hot.ee> - 2016-02-22 15:16 -0800
          Re: String integer id Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-22 19:54 -0500
            Re: String integer id "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2016-02-23 02:26 +0100
              Re: String integer id Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-22 20:35 -0500
                Re: String integer id legalize+jeeves@mail.xmission.com (Richard) - 2016-02-23 02:31 +0000
                Re: String integer id Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-22 21:42 -0500
                Re: String integer id "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2016-02-23 04:59 +0100
                Re: String integer id Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-23 08:32 -0500
                Re: String integer id "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2016-02-23 03:34 +0100
                Re: String integer id Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-22 21:38 -0500
                Re: String integer id Google <google@google.com> - 2016-02-22 19:36 -0800
                Re: String integer id David Brown <david.brown@hesbynett.no> - 2016-02-23 09:25 +0100
                Re: String integer id Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-23 08:33 -0500
                Re: String integer id David Brown <david.brown@hesbynett.no> - 2016-02-23 15:43 +0100
                Re: String integer id Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-23 11:21 -0500
                Re: String integer id woodbrian77@gmail.com - 2016-02-22 20:48 -0800
                Re: String integer id David Brown <david.brown@hesbynett.no> - 2016-02-23 09:16 +0100
                Re: String integer id Paavo Helde <myfirstname@osa.pri.ee> - 2016-02-23 18:23 +0200
                Re: String integer id David Brown <david.brown@hesbynett.no> - 2016-02-23 22:29 +0100
  Re: String integer id Barry Schwarz <schwarzb@dqel.com> - 2016-02-22 11:41 -0800
    Re: String integer id Ian Collins <ian-news@hotmail.com> - 2016-02-23 09:13 +1300
      Re: String integer id Paavo Helde <myfirstname@osa.pri.ee> - 2016-02-23 00:32 +0200
        Re: String integer id legalize+jeeves@mail.xmission.com (Richard) - 2016-02-22 22:39 +0000
          Re: String integer id "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2016-02-23 00:46 +0100
          Re: String integer id Paavo Helde <myfirstname@osa.pri.ee> - 2016-02-23 10:01 +0200
            Re: String integer id "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2016-02-23 19:15 +0100
              Re: String integer id Paavo Helde <myfirstname@osa.pri.ee> - 2016-02-23 21:40 +0200
  Re: String integer id Juha Nieminen <nospam@thanks.invalid> - 2016-02-23 09:18 +0000
    Re: String integer id malcolm.mclean5@btinternet.com - 2016-02-23 03:15 -0800

csiph-web