C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Ternary typedef's

From: Wilhelm M <wilhelm.now_at_[hidden]>
Date: Wed, 20 Jul 2022 11:34:44 +0200
Hi,

Have you considered std::conditional(_t)?


On Wed, Jul 20, 2022, 11:28 Frederick Virchanza Gotham via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> 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;
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2022-07-20 09:34:56