B Revzin - if const expr isn't broken (was Re: My Meeting C++ Keynote video is now available)

Walter Bright newshound2 at digitalmars.com
Thu Jan 17 01:14:29 UTC 2019


On 1/16/2019 3:43 PM, John Carter wrote:
> Somebody on the C++ side has written a reply....
> 
> https://brevzin.github.io/c++/2019/01/15/if-constexpr-isnt-broken/

 From the article:

D (with corrections):

   static if (maxLength < 0xFFFE) {
     alias CellIdx = uint16_t;
   } else {
     alias CellIdx = uint32_t;
   }

C++:

   static constexpr auto get_type() {
     if constexpr (maxLength < 0xFFFE) {
         return type<uint16_t>;
     } else {
         return type<uint32_t>;
     }
   }

   using CellIdx = decltype(get_type())::type;

1. you've got to write a function separately for every declaration you want to 
declare in a conditional. Imagine doing 4 or 5 of these.

2. imagine this:

     static if (condition)
       int x;
     ...
     static if (condition)
       ++x;

The C++ idiom would require (along with creating another function) creating a 
dummy x declaration for the (omitted) else branch of the static if.
Doable, but ugly. It kinda reminds me of the C diehards who showed you can write 
virtual function dispatch in C.

It's remarkable that these things can be done in C++, but the amount of "noise" 
and "boilerplate" in the solutions make them pretty hard to read.


More information about the Digitalmars-d-announce mailing list