C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Explicit keyword for template parameters

From: Jens Maurer <Jens.Maurer_at_[hidden]>
Date: Sat, 30 Apr 2022 11:39:46 +0200
On 30/04/2022 10.07, Frederick Virchanza Gotham via Std-Proposals wrote:> When a programmer uses my> function "Add_Or_Max", I want it to stick out like a sore thumb in the> code which integer type they're using, e.g.:
>
> #include <cstdint>
> using std::uint16_t;
>
> uint16_t my_global_var;
>
> int main(void)
> {
> Add_Or_Max( my_global_var, 52u ); // I don't want this to compiler
>
> Add_Or_Max<uint16_t>( my_global_var, 52u ); // I want this to compile
> }

If you want to disable template argument deduction for a function parameter,
just do it (see [meta.trans.other]):

template<class T>
/* whatever */ Add_Or_Max(std::type_identity_t<T> const a, std::type_identity_t<T> const b);

Since T only appears in non-deduced contexts, a user needs to specify
it explicitly in the template-argument-list of the function call.

Jens

Received on 2022-04-30 09:39:50