Boost logo

Boost :

Subject: Re: [boost] [config/multiprecision/units/general] Do we have a policy for user-defined-literals?
From: John Maddock (john_at_[hidden])
Date: 2013-04-28 08:46:24


>> Note however, that constructors may be less efficient in general -
>> cpp_int users would have to fall back on a construct-from-string rather
>> than constexpr initialisation (the issue is you can't write a number with
>> enough digits unless it has a user-defined-suffix).
>
> Can't you construct from string constexpr? Both gcc and clang are happy
> with code like this (just an experiment to see what constexpr accepts):
>
> struct uint128 {
> unsigned long h, l;
> constexpr uint128(unsigned long h_, unsigned long l_):h(h_),l(l_){}
> constexpr uint128 lshift(int i)const{
> return uint128{h<<4|l>>60,l<<4|i};
> }
> static constexpr int chartoint(char c){
> return (c>='0'&&c<='9')?c-'0':(c-'a'+10);
> }
> static constexpr uint128 from_string(uint128 tmp, const char* s){
> return (*s==0)?tmp:from_string(tmp.lshift(chartoint(*s)),s+1);
> }
> };
>
> int main(){
> constexpr uint128
> a=uint128::from_string(uint128{0,0},"1234567890abcdef123");
> static_assert(a.l==0x4567890abcdef123,"");
> static_assert(a.h==0x123,"");
> }

I had no idea you could do that! It's still a lot easier to code a
user-defined-literal though ;-)

Thanks, John.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk