C++ Logo

std-proposals

Advanced search

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

From: Hui Xie <hui.xie1990_at_[hidden]>
Date: Fri, 25 Mar 2022 10:58:10 +0000
not related to your proposals, but you could use `std::type_identity_t<T>`
in your example.

Regards,
Hui

On Fri, Mar 25, 2022 at 10:47 AM Frederick Virchanza Gotham via
Std-Proposals <std-proposals_at_[hidden]> wrote:

> Let's say I have a template function as follows:
>
> template<class T>
> void Func(T &arg)
> {
> /* Do Something */
> }
>
> And let's say that my function can be used in a program as follows:
>
> #include <iostream>
>
> int main(void)
> {
> Func(std::cout);
> }
>
> However I don't want my function to be used like this. I want the
> programmer to have to explicitly specify the type, as follows:
>
> Func<ostream>(std::cout);
>
> In order to impose this restriction, I have to rewrite my template
> function as follows:
>
> #include <type_traits>
>
> template <class T>
> void Func(typename std::common_type<T>::type &obj)
> {
> /* Do Something */
> }
>
> After having made this change, the following line no longer compiles:
>
> Func(std::cout);
>
> and this is exactly the behaviour I want.
>
> I think we should make use of the "explicit" keyword to disable
> argument-based template parameter deduction, as follows:
>
> template<explicit class T>
> void Func(T &arg)
> {
> /* Do Something */
> }
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2022-03-25 10:58:24