Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #41106 > unrolled thread
| Started by | malcolm.mclean5@btinternet.com |
|---|---|
| First post | 2016-02-22 06:59 -0800 |
| Last post | 2016-02-23 03:15 -0800 |
| Articles | 15 on this page of 35 — 13 participants |
Back to article view | Back to comp.lang.c++
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
Page 2 of 2 — ← Prev page 1 [2]
| From | Jerry Stuckle <jstucklex@attglobal.net> |
|---|---|
| Date | 2016-02-23 11:21 -0500 |
| Message-ID | <nai0p6$s2m$3@jstuckle.eternal-september.org> |
| In reply to | #41164 |
On 2/23/2016 9:43 AM, David Brown wrote: > On 23/02/16 14:33, Jerry Stuckle wrote: >> On 2/23/2016 3:25 AM, David Brown wrote: >>> On 23/02/16 04:36, Google wrote: >>>> On Mon, 22 Feb 2016 21:38:02 -0500, Jerry Stuckle >>>> <jstucklex@attglobal.net> wrote: >>>> >>>>> And, of course, everything I find on the internet is the truth, the >>>>> whole truth, and nothing but the truth. So help me Google. >>>> >>> <snip> >>> >>> I don't know who posted this, but it is inappropriate behaviour. It is >>> fine to condemn a posters style, and it is fine to be so frustrated with >>> childish posts that you killfile them. But it is /not/ fine to post >>> personal information, even if it is already available to anyone with a >>> web browser. >>> >>> In a Usenet group, you accept people as they come, or you argue with >>> them, or you ignore them. Posts like this are harassment. >>> >>> >> >> It's just what is expected from the trolls here. And this one is a very >> dumb troll, at that. >> > > Trolls, no matter what reasonable and common definition you use, have > been very rare in this group (your opinion on who is or is not a troll > matters little when it is so contrary to the rest of the group). > Amongst the mostly on-topic and helpful posts, we get some pointless > posts, some irritating posts, and occasionally rude posts, but seldom > trolling posts. > > No, the post here was /not/ what I expect from people in this group - > that is why I react against it. I don't think it really counts as > trolling either. I am not sure /what/ it counts as, but I don't like it. > It is trolling, plain and simple, according to the commonly used definition for the last 20+ years. As are twisting people's words, ignoring facts someone else posts and other things I've seen in this group. Maybe you don't think it's trolling. But this post does not surprise me in the least. And the poster was even too chicken to use his own name. Just another proof of trolls being here. -- ================== Remove the "x" from my email address Jerry Stuckle jstucklex@attglobal.net ==================
[toc] | [prev] | [next] | [standalone]
| From | woodbrian77@gmail.com |
|---|---|
| Date | 2016-02-22 20:48 -0800 |
| Message-ID | <ac120d60-c34d-423d-9abf-8abc07d32755@googlegroups.com> |
| In reply to | #41148 |
On Monday, February 22, 2016 at 8:38:12 PM UTC-6, Jerry Stuckle wrote: > > And, of course, everything I find on the internet is the truth, the > whole truth, and nothing but the truth. So help me Google. > How about https://duckduckgo.com ? They don't track you and in my opinion are more trustworthy than Google. Brian Ebenezer Enterprises - In G-d we trust. http://webEbenezer.net
[toc] | [prev] | [next] | [standalone]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Date | 2016-02-23 09:16 +0100 |
| Message-ID | <nah4ak$hl6$1@dont-email.me> |
| In reply to | #41145 |
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.
Most toolchains have some sort of pre-defined compiler macros or a
header file with macros that can be used to determine endianness (like
gcc's __BYTE_ORDER__ and __ORDER_LITTLE_ENDIAN__, so there are plenty of
implementation-specific methods.
A compile-time constant expression that relies on nothing beyond the
standards would be fascinating - please post it here.
[toc] | [prev] | [next] | [standalone]
| From | Paavo Helde <myfirstname@osa.pri.ee> |
|---|---|
| Date | 2016-02-23 18:23 +0200 |
| Message-ID | <7LednbJiLbgVGlHLnZ2dnUU78KGdnZ2d@giganews.com> |
| In reply to | #41155 |
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?
[toc] | [prev] | [next] | [standalone]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Date | 2016-02-23 22:29 +0100 |
| Message-ID | <naiipd$kkh$1@dont-email.me> |
| In reply to | #41168 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Barry Schwarz <schwarzb@dqel.com> |
|---|---|
| Date | 2016-02-22 11:41 -0800 |
| Message-ID | <gmnmcbph3ui9hga32drdehpdfior35d6nr@4ax.com> |
| In reply to | #41106 |
On Mon, 22 Feb 2016 06:59:14 -0800 (PST),
malcolm.mclean5@btinternet.com wrote:
>I want a flyweight class that is a "string id".
>That is to say, it consists of a 64 bit number which is also a string of up to seven
>characters, used for passing about human-readable ids that the machine can
>process as integers.
>
>However how do I write it so that it's easy to make it a constexpr, whilst still being
>convenient to call. (The ids will mostly be known at compile time, only rarely will
>you have a situation when the user enters a string and it ends up being turned
>into a dynamic id).
>Particularly I would like to be able to switch on the id and have human-readable names
>in the case conditions.
For values known at compile time, #define should do what you want. For
example, the name Ann is 0x41 for the A and 0x6e for each n so
#define Al 0x416e6e /* or possibly 0x416e6e0000000000 */
will allow you to code
switch (id){
case Ann:
...
--
Remove del for email
[toc] | [prev] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2016-02-23 09:13 +1300 |
| Message-ID | <dj18fbFj7j6U10@mid.individual.net> |
| In reply to | #41113 |
Barry Schwarz wrote: > On Mon, 22 Feb 2016 06:59:14 -0800 (PST), > malcolm.mclean5@btinternet.com wrote: > >> I want a flyweight class that is a "string id". >> That is to say, it consists of a 64 bit number which is also a string of up to seven >> characters, used for passing about human-readable ids that the machine can >> process as integers. >> >> However how do I write it so that it's easy to make it a constexpr, whilst still being >> convenient to call. (The ids will mostly be known at compile time, only rarely will >> you have a situation when the user enters a string and it ends up being turned >> into a dynamic id). >> Particularly I would like to be able to switch on the id and have human-readable names >> in the case conditions. > > For values known at compile time, #define should do what you want. This is comp.lang.c++, cont comp.lang.c! -- Ian Collins
[toc] | [prev] | [next] | [standalone]
| From | Paavo Helde <myfirstname@osa.pri.ee> |
|---|---|
| Date | 2016-02-23 00:32 +0200 |
| Message-ID | <OYOdncUI350MEVbLnZ2dnUU78N-dnZ2d@giganews.com> |
| In reply to | #41120 |
On 22.02.2016 22:13, Ian Collins wrote:
> Barry Schwarz wrote:
>> On Mon, 22 Feb 2016 06:59:14 -0800 (PST),
>> malcolm.mclean5@btinternet.com wrote:
>>
>>> I want a flyweight class that is a "string id".
>>> That is to say, it consists of a 64 bit number which is also a string
>>> of up to seven
>>> characters, used for passing about human-readable ids that the
>>> machine can
>>> process as integers.
>>>
>>> However how do I write it so that it's easy to make it a constexpr,
>>> whilst still being
>>> convenient to call. (The ids will mostly be known at compile time,
>>> only rarely will
>>> you have a situation when the user enters a string and it ends up
>>> being turned
>>> into a dynamic id).
>>> Particularly I would like to be able to switch on the id and have
>>> human-readable names
>>> in the case conditions.
>>
>> For values known at compile time, #define should do what you want.
>
> This is comp.lang.c++, cont comp.lang.c!
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,
};
std::string StrFromId(uint64_t x) {
std::string s(reinterpret_cast<const char*>(&x), 8);
return s.substr(0, s.find(char(0)));
}
int main() {
uint64_t arr[] = {Ann, Betty};
for(auto x: arr) {
switch(x) {
case Ann:
std::cout << "A " << StrFromId(x) << "\n";
break;
case Betty:
std::cout << "B " << StrFromId(x) << "\n";
break;
}
}
}
Output (little-endian):
A Ann
B Betty
[toc] | [prev] | [next] | [standalone]
| From | legalize+jeeves@mail.xmission.com (Richard) |
|---|---|
| Date | 2016-02-22 22:39 +0000 |
| Message-ID | <nag2ns$i4i$1@news.xmission.com> |
| In reply to | #41136 |
[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?
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
[toc] | [prev] | [next] | [standalone]
| From | "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> |
|---|---|
| Date | 2016-02-23 00:46 +0100 |
| Message-ID | <nag6fd$6tb$1@dont-email.me> |
| In reply to | #41137 |
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
[toc] | [prev] | [next] | [standalone]
| From | Paavo Helde <myfirstname@osa.pri.ee> |
|---|---|
| Date | 2016-02-23 10:01 +0200 |
| Message-ID | <wdednWE50-xDjFHLnZ2dnUU78eGdnZ2d@giganews.com> |
| In reply to | #41137 |
On 23.02.2016 0: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?
>
Yes, you are right. The problem with endianness is that the bytes are
not guaranteed to be in a strictly monotonic order. Others are claiming
that there is no standard and reliable way to find the endianness out at
compile time (for constexpr). However, I started to think isn't it
possible to reuse the user literal mechanism itself, even if it is not
good for finding out the actual endianness it should be good for the
user literals themselves.
(Warning: I do not have access to any big- or mixed-endian machines for
testing, so this may contain bugs, and certainly it can be simplified):
#include <iostream>
#include <string>
#include <stdint.h>
constexpr uint64_t operator""_idraw(const char* p, size_t n) {
return static_cast<unsigned char>(*p) | (n>0 ? operator""_idraw(p+1,
n-1): uint64_t(0))<<8;
}
constexpr uint64_t Fill64(uint64_t c) {
return c | c<<8 | c<<16 | c<<24 | c<<32 | c<<40 | c<<48 | c<<56;
}
constexpr uint64_t SelectByte(int pos) {
return
pos==0 ? "\xff"_idraw :
(pos==1 ? "\x00\xff"_idraw :
(pos==2 ? "\x00\x00\xff"_idraw :
(pos==3 ? "\x00\x00\x00\xff"_idraw :
(pos==4 ? "\x00\x00\x00\x00\xff"_idraw :
(pos==5 ? "\x00\x00\x00\x00\x00\xff"_idraw :
(pos==6 ? "\x00\x00\x00\x00\x00\x00\xff"_idraw :
(pos==7 ? "\x00\x00\x00\x00\x00\x00\x00\xff"_idraw : 0)))))));
}
constexpr uint64_t IdFromString(const char* p, size_t n, int pos) {
return (Fill64(static_cast<unsigned char>(*p)) & SelectByte(pos)) |
(n>0 ? IdFromString(p+1, n-1, pos+1) : uint64_t(0));
}
constexpr uint64_t operator""_id(const char* p, size_t n) {
return IdFromString(p, n, 0);
}
enum constants: uint64_t {
Ann = "Ann"_id,
Betty = "Betty"_id,
};
std::string StrFromId(uint64_t x) {
std::string s(reinterpret_cast<const char*>(&x), 8);
return s.substr(0, s.find(char(0)));
}
int main() {
uint64_t arr[] = {Ann, Betty, "Caroline"_id, "Delilah"_id};
for(auto x: arr) {
switch(x) {
case Ann:
std::cout << "A " << StrFromId(x) << "\n";
break;
case Betty:
std::cout << "B " << StrFromId(x) << "\n";
break;
case "Caroline"_id:
std::cout << "C " << StrFromId(x) << "\n";
break;
case "Delilah"_id:
std::cout << "D " << StrFromId(x) << "\n";
break;
}
}
}
[toc] | [prev] | [next] | [standalone]
| From | "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> |
|---|---|
| Date | 2016-02-23 19:15 +0100 |
| Message-ID | <nai7ef$q4n$1@dont-email.me> |
| In reply to | #41154 |
On 23.02.2016 09:01, Paavo Helde wrote:
>
> [snip] The problem with endianness is that the bytes are
> not guaranteed to be in a strictly monotonic order. Others are claiming
> that there is no standard and reliable way to find the endianness out at
> compile time (for constexpr). However, I started to think isn't it
> possible to reuse the user literal mechanism itself, even if it is not
> good for finding out the actual endianness it should be good for the
> user literals themselves.
If that were possible then it could serve as a way to determine
endianness, which would be great.
> (Warning: I do not have access to any big- or mixed-endian machines for
> testing, so this may contain bugs, and certainly it can be simplified):
I suspect that this is a late-night posting.
I sometimes do that. ;-)
Happily the net is full of critics.
> #include <iostream>
> #include <string>
> #include <stdint.h>
>
> constexpr uint64_t operator""_idraw(const char* p, size_t n) {
> return static_cast<unsigned char>(*p) | (n>0 ?
> operator""_idraw(p+1, n-1): uint64_t(0))<<8;
> }
>
> constexpr uint64_t Fill64(uint64_t c) {
> return c | c<<8 | c<<16 | c<<24 | c<<32 | c<<40 | c<<48 | c<<56;
> }
>
> constexpr uint64_t SelectByte(int pos) {
> return
> pos==0 ? "\xff"_idraw :
> (pos==1 ? "\x00\xff"_idraw :
> (pos==2 ? "\x00\x00\xff"_idraw :
> (pos==3 ? "\x00\x00\x00\xff"_idraw :
> (pos==4 ? "\x00\x00\x00\x00\xff"_idraw :
> (pos==5 ? "\x00\x00\x00\x00\x00\xff"_idraw :
> (pos==6 ? "\x00\x00\x00\x00\x00\x00\xff"_idraw :
> (pos==7 ? "\x00\x00\x00\x00\x00\x00\x00\xff"_idraw : 0)))))));
> }
>
> constexpr uint64_t IdFromString(const char* p, size_t n, int pos) {
> return (Fill64(static_cast<unsigned char>(*p)) & SelectByte(pos)) |
> (n>0 ? IdFromString(p+1, n-1, pos+1) : uint64_t(0));
> }
>
> constexpr uint64_t operator""_id(const char* p, size_t n) {
> return IdFromString(p, n, 0);
> }
>
> enum constants: uint64_t {
> Ann = "Ann"_id,
> Betty = "Betty"_id,
> };
>
> std::string StrFromId(uint64_t x) {
> std::string s(reinterpret_cast<const char*>(&x), 8);
> return s.substr(0, s.find(char(0)));
> }
>
>
> int main() {
>
> uint64_t arr[] = {Ann, Betty, "Caroline"_id, "Delilah"_id};
>
> for(auto x: arr) {
> switch(x) {
> case Ann:
> std::cout << "A " << StrFromId(x) << "\n";
> break;
> case Betty:
> std::cout << "B " << StrFromId(x) << "\n";
> break;
> case "Caroline"_id:
> std::cout << "C " << StrFromId(x) << "\n";
> break;
> case "Delilah"_id:
> std::cout << "D " << StrFromId(x) << "\n";
> break;
> }
> }
> }
Okay, let's first examine
constexpr uint64_t operator""_idraw(const char* p, size_t n) {
return static_cast<unsigned char>(*p) | (n>0 ?
operator""_idraw(p+1, n-1): uint64_t(0))<<8;
}
constexpr uint64_t SelectByte(int pos) {
return
pos==0 ? "\xff"_idraw :
(pos==1 ? "\x00\xff"_idraw :
(pos==2 ? "\x00\x00\xff"_idraw :
(pos==3 ? "\x00\x00\x00\xff"_idraw :
(pos==4 ? "\x00\x00\x00\x00\xff"_idraw :
(pos==5 ? "\x00\x00\x00\x00\x00\xff"_idraw :
(pos==6 ? "\x00\x00\x00\x00\x00\x00\xff"_idraw :
(pos==7 ? "\x00\x00\x00\x00\x00\x00\x00\xff"_idraw : 0)))))));
}
This can be replaced with just
constexpr uint64_t SelectByte(int pos) {
return (pos >= 8? 0 : uint64_t( 0xFF ) << 8*pos);
}
That's a good code transformation, I think. :)
Then this:
constexpr uint64_t Fill64(uint64_t c) {
return c | c<<8 | c<<16 | c<<24 | c<<32 | c<<40 | c<<48 | c<<56;
}
constexpr uint64_t SelectByte(int pos) {
return (pos >= 8? 0 : uint64_t( 0xFF ) << 8*pos);
}
constexpr uint64_t IdFromString(const char* p, size_t n, int pos) {
return (Fill64(static_cast<unsigned char>(*p)) &
SelectByte(pos)) | (n>0 ? IdFromString(p+1, n-1, pos+1) : uint64_t(0));
… can be replaced with just
constexpr
auto left_shifted( unsigned char const val, int const distance )
-> uint64_t
{ return (distance >= 64? 0 : uint64_t( val ) << distance); }
constexpr uint64_t IdFromString(const char* p, size_t n, int pos) {
return left_shifted( *p, 8*pos ) | (n>0 ? IdFromString(p+1,
n-1, pos+1) : uint64_t(0));
}
And at this point it's clear that the code does an endian-agnostic
conversion from a base 256 number specification, to number, just as the
earlier programs.
• • •
The endianness issue can instead be addressed in the `StrFromId`
function, where one is not constrained by the `constexpr` limitations.
E.g. a strategic little call to `std::reverse` will do the trick.
Cheers!,
- Alf
[toc] | [prev] | [next] | [standalone]
| From | Paavo Helde <myfirstname@osa.pri.ee> |
|---|---|
| Date | 2016-02-23 21:40 +0200 |
| Message-ID | <_NudndoGpsgyKFHLnZ2dnUU78VGdnZ2d@giganews.com> |
| In reply to | #41171 |
On 23.02.2016 20:15, Alf P. Steinbach wrote: > On 23.02.2016 09:01, Paavo Helde wrote: >> >> [snip] The problem with endianness is that the bytes are >> not guaranteed to be in a strictly monotonic order. Others are claiming >> that there is no standard and reliable way to find the endianness out at >> compile time (for constexpr). However, I started to think isn't it >> possible to reuse the user literal mechanism itself, even if it is not >> good for finding out the actual endianness it should be good for the >> user literals themselves. > > If that were possible then it could serve as a way to determine > endianness, which would be great. > > >> (Warning: I do not have access to any big- or mixed-endian machines for >> testing, so this may contain bugs, and certainly it can be simplified): > > I suspect that this is a late-night posting. No, this was an early-morning posting before sufficient coffee intake ;-) > > And at this point it's clear that the code does an endian-agnostic > conversion from a base 256 number specification, to number, just as the > earlier programs. Yeah, I was having a growing suspicion of the same ... > The endianness issue can instead be addressed in the `StrFromId` > function, where one is not constrained by the `constexpr` limitations. True, but I think someone wanted to see the strings in memory while debugging. But for that purpose the endianness is in practice easily detected by the relevant macros and in the remote chance the program gets it wrong then it is easy enough to fix and recompile. Anyway, thanks for demolishing my monster! Paavo
[toc] | [prev] | [next] | [standalone]
| From | Juha Nieminen <nospam@thanks.invalid> |
|---|---|
| Date | 2016-02-23 09:18 +0000 |
| Message-ID | <nah84f$47g$2@adenine.netfront.net> |
| In reply to | #41106 |
malcolm.mclean5@btinternet.com wrote: > That is to say, it consists of a 64 bit number which is also a string of up to seven > characters, used for passing about human-readable ids that the machine can > process as integers. Given that 64 bits is 8 bytes, it is physically impossible to represent a 64-bit value with 7 characters. (The fact that you want it to be readable reduces the range even more.) --- news://freenews.netfront.net/ - complaints: news@netfront.net ---
[toc] | [prev] | [next] | [standalone]
| From | malcolm.mclean5@btinternet.com |
|---|---|
| Date | 2016-02-23 03:15 -0800 |
| Message-ID | <09b221a2-04e9-47ed-9d3f-eda01fe4d269@googlegroups.com> |
| In reply to | #41158 |
On Tuesday, February 23, 2016 at 9:18:17 AM UTC, Juha Nieminen wrote: > malcolm.mclean5@btinternet.com wrote: > > That is to say, it consists of a 64 bit number which is also a string of up to seven > > characters, used for passing about human-readable ids that the machine can > > process as integers. > > Given that 64 bits is 8 bytes, it is physically impossible to represent a > 64-bit value with 7 characters. (The fact that you want it to be readable > reduces the range even more.) > We're not representing 64 bit values with 7-character string, we're representing 7 character strings as 64 bit values.
[toc] | [prev] | [standalone]
Page 2 of 2 — ← Prev page 1 [2]
Back to top | Article view | comp.lang.c++
csiph-web