C++ Logo

std-proposals

Advanced search

Re: Two small ideas

From: Timur Doumler <cpp_at_[hidden]>
Date: Wed, 8 Dec 2021 20:37:28 +0200
Hi Ville,

Agreed, we should totally sync our efforts.

Apart from allowing CTAD in more places, I’d like to allow “partial CTAD”, i.e. declare the first N template parameters of an object of class template type but let the compiler deduce the rest, for example:

 std::array<float, …> arr = {1, 2, 3, 4, 5};

here, I specify the element type but let the compiler deduce the array size (there are more interesting use cases for this).

Function templates have already been doing this since pre C++98, you can write:

 auto t = std::make_tuple<int>(24, false); // int specified, bool deduced

For classes and CTAD, we can’t just allow this without extra syntax, because otherwise we run into all kinds of ambiguities and potential code breakage (I have examples for this), therefore the `…` which is one possible such extra syntax.

The print_matrix use case also needs a new syntax to say “please deduce template arguments here”. It would be nice to harmonise this and not invent multiple new syntaxes.

Cheers,
Timur


> On 8 Dec 2021, at 20:17, Ville Voutilainen <ville.voutilainen_at_[hidden]> wrote:
>
> On Wed, 8 Dec 2021 at 20:07, Timur Doumler <cpp_at_[hidden]> wrote:
>>
>> There is some overlap here with the idea of making CTAD work for function arguments (and in other contexts where it is currently not allowed), which is something I am considering proposing for post-C++23.
>
> Sure, I have run into that. We should perhaps sync these efforts, a
> CTAD parameter needs to not drop the indication
> that the function is a template, and deduced but type-constrained
> parameters need to fit, and then it all needs to fit
> into how variable declarations are done.
>
> The syntax we chose for CTADed variables kinda blows, but at least it
> doesn't restrict the syntactic design space, much.
> It's non-reusable for parameters, though, because it completely loses
> the "hey you're looking at a template here, beware"
> bit of information.

Received on 2021-12-08 12:37:34