C++ Logo

std-proposals

Advanced search

[std-proposals] Ternary typedef's

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Wed, 20 Jul 2022 10:28:43 +0100
Right now with C++20 we can use the preprocessor to select different
types, as follows:

          #define USE_DEBUGGING_CONTAINERS

          #ifdef USE_DEBUGGING_CONTAINERS
             using __gnu_debug::vector;
          #else
             using std::vector;
          #endif

However it would be nice if we could use the ternary operator with
types in a few different contexts, for example:

          bool constexpr use_debugging_containers = true;

          using (use_debugging_containers ? __gnu_debug::vector : std::vector );

          typedef (use_debugging_containers ? __gnu_debug::vector<int>
: std::vector<int> ) IntVec;

The closest thing we have to this at the moment is a lot more complicated:

          bool constexpr use_debugging_containers = true;

          namespace Containers {

              template<bool x>
              class detail {
                  template<typename... Args>
                  using vector = std::vector<Args...>;
              };

              template<>
              class detail<true> {
                          template<typename... Args>
                          using vector = __gnu_debug::vector<Args...>;
              };

              template<typename... Args>
              using vector = detail<use_debugging_containers>::vector<Args...>;
          }

          using namespace Containers;

          int main(void)
          {
              vector<double> vec;
          }

Also I propose that the ternaries can be nested:

          typedef ( number > 65535u ? uint32_t : ( number > 255u ?
uint16_t : uint8_t ) ) IntT;

Received on 2022-07-20 09:28:55