|
Boost : |
Subject: Re: [boost] [Safe Numerics] Review
From: Robert Ramey (ramey_at_[hidden])
Date: 2017-03-12 15:56:26
On 3/12/17 7:54 AM, Peter Dimov via Boost wrote:
> John Maddock wrote:
>
>> 5) What is the purpose of class safe_literal? constepxr initialization
>> seems to work just fine without it?
>
> The idea, I believe, is that if you have
>
> safe_signed_range<0, 100> x;
>
> and then you do
>
> auto y = x * safe_signed_literal<2>();
>
> you get safe_signed_range<0, 200> as the type of y. This could probably
> be made less elaborate with a user-defined literal, for example
>
> auto y = x * 2_sf;
>
> or something like that.
>
> Since x is not constexpr, it's not possible (I think) to achieve this
> result without using a separate literal type to hold the compile-time
> constant 2.
here is the problem:
constexpr int i = 42; // i is constexpr and available at compile time
constexpr const safe_int x(i); // x is constexpr and available at
compile time
constexpr const safe_int y(42); // y is NOT available at compile time!!!
constexpr const safe_int z(safe_signed_literal<42>()); // z is NOW
available at compile
So the problem is that literals are not considered constexpr. I believe
this is a problem is with the way constexpr is defined.
Actually I have a whole rant on constexpr and const expressions. I
believe that C++ standard has made this a lot more complex and less
useful than it could be. But I can pursue only one hopeless quest at at
time.
Robert Ramey
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk