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


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

Re: String integer id

From "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>
Newsgroups comp.lang.c++
Subject Re: String integer id
Date 2016-02-23 00:46 +0100
Organization A noiseless patient Spider
Message-ID <nag6fd$6tb$1@dont-email.me> (permalink)
References <224a55e1-bcba-4063-915d-02f0b53dd294@googlegroups.com> <gmnmcbph3ui9hga32drdehpdfior35d6nr@4ax.com> <dj18fbFj7j6U10@mid.individual.net> <OYOdncUI350MEVbLnZ2dnUU78N-dnZ2d@giganews.com> <nag2ns$i4i$1@news.xmission.com>

Show all headers | View raw


On 22.02.2016 23:39, Richard wrote:
> [Please do not mail me a copy of your followup]
>
> Paavo Helde <myfirstname@osa.pri.ee> spake the secret code
> <OYOdncUI350MEVbLnZ2dnUU78N-dnZ2d@giganews.com> thusly:
>
>> No problem, in C++ we can have the same:
>>
>> enum constants: uint64_t {
>> 	Ann = 0x6e6e41,
>> 	Betty = 0x7974746542,
>> };
>>
>> Or even combine this with user literals:
>>
>> #include <iostream>
>> #include <string>
>> #include <stdint.h>
>>
>> constexpr uint64_t operator""_id(const char* p, size_t n) {
>> 	return static_cast<unsigned char>(*p) | (n>0 ? operator""_id(p+1, n-1)
>> : uint64_t(0))<<8;
>> }
>>
>> enum constants: uint64_t {
>> 	Ann = "Ann"_id,
>> 	Betty = "Betty"_id,
>> };
>
> For this to be visible as the string "Ann\0" in memory, doesn't this
> assume a certain byte order for integral types?
>

I think Paavo's code is way too portable, so:

     #include <iostream>
     #include <string>
     #include <stdint.h>
     using namespace std;

     constexpr
     auto adjoined( uint64_t const a, uint64_t const b )
         -> uint64_t
     { return (a << 32) | b; }

     enum constants: uint64_t { Ann = 'nnA', Betty = adjoined( 'y', 
'tteB' ) };

     auto str_from_id( uint64_t const x )
         -> std::string
     { return reinterpret_cast<const char*>( &x ); }

     auto main() -> int
     {
         uint64_t const arr[] = {Ann, Betty};
         for( auto const x: arr ) switch( x )
         {
             case Ann:
                 cout << "A " << str_from_id( x ) << "\n";
                 break;
             case Betty:
                 cout << "B " << str_from_id( x ) << "\n";
                 break;
         }
     }


This avoids using newfangled stuff like C++11 user defined literals, and 
leverages the good old C syntax that BEEN THERE FOR A REASON since, 
well, since the 1970s!

After all, what could possibly go wrong?


Cheers!,

- Alf

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