C++ Logo

std-proposals

Advanced search

[std-proposals] New attribute to add implicit using statements for template parameter lists

From: Ryan Klaus <rfklaus117_at_[hidden]>
Date: Sun, 27 Nov 2022 19:40:00 -0600
Let's say I have a template class that takes two enums:

template <ColorComponents T_components, ColorComponentLayout T_layout>
class Color { ... };


Because I'm a good boy, each enum is of course an enum class like so:

enum class ColorComponentLayout
{
    _332,
    _4444,
    ...
};

enum class ColorComponents
{
    RGB,
    RGBA,
    ARGB,
    ...
};


And all of the above is inside the namespace "ABC".

In a just world, I feel like I should be able to right something like this:

auto color = ABC::Color<RGBA, _8888>{};


The compiler knows it wants a *ColorComponents *followed by a
*ColorComponentLayout*, so all of the contextual information is
theoretically there.
But this is not a just world and so I'm forced to write this incantation:

auto color = ABC::Color<ABC::ColorComponents::RGBA,
ABC::ColorComponentLayout::_8888>{};


So my proposal is to create some new attribute, [[context(...)]], such that
we could write:

[[context(using enum ColorComponents, using enum ColorComponentLayout)]]

template <ColorComponents T_components, ColorComponentLayout T_layout>
class Color { ... };


This essentially allows for any parameter list (function or template) to
have using statements scoped only to the parameter list itself. This could
also permit type aliases to allow for context specific parameter list
aliases. Not sure when that would be useful, but I could see that
potentially being a very handy space saver.

What's everyone's thoughts on this? Is it possible? Is it not possible? Is
it possible but we hate it?

Thanks,
Ryan Klaus

Received on 2022-11-28 01:40:13