Date: Sun, 1 May 2022 13:06:38 -0400
We have `= delete` for removing unwanted conversions. Indeed, you can
basically force the use of `int` with:
template<typename T>
int func (int, T) = delete;
Any call to `func` that doesn't pass an `int` as the second parameter
will fail to compile. Even things convertible to `int` will not be
used.
If you want to allow more things to call it, you can only cull out
floating-point types with this overload:
int func(int, double) = delete;
Calling func(5, 1) will work, but func(5, 3.4) or even func(5, 2.3f)
will fail. This preserves the ability to call it with types
convertible to `int` (but those which are convertible to `int` *and*
floats will fail due to ambiguity).
On Sun, May 1, 2022 at 3:57 AM Abdullah Qasim via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
>
>
> int func (int var, explicit int select_menu)
>
> {
>
> /// ...
>
> }
>
>
>
> Why would I want user to put a double value in select_menu ???
>
>
>
> Also, why would I want to disallow a double in var ???
>
>
>
> The solution:
>
> Allow explicit in param list!!
>
>
>
> Not too hard to implement, either.
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
basically force the use of `int` with:
template<typename T>
int func (int, T) = delete;
Any call to `func` that doesn't pass an `int` as the second parameter
will fail to compile. Even things convertible to `int` will not be
used.
If you want to allow more things to call it, you can only cull out
floating-point types with this overload:
int func(int, double) = delete;
Calling func(5, 1) will work, but func(5, 3.4) or even func(5, 2.3f)
will fail. This preserves the ability to call it with types
convertible to `int` (but those which are convertible to `int` *and*
floats will fail due to ambiguity).
On Sun, May 1, 2022 at 3:57 AM Abdullah Qasim via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
>
>
> int func (int var, explicit int select_menu)
>
> {
>
> /// ...
>
> }
>
>
>
> Why would I want user to put a double value in select_menu ???
>
>
>
> Also, why would I want to disallow a double in var ???
>
>
>
> The solution:
>
> Allow explicit in param list!!
>
>
>
> Not too hard to implement, either.
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
Received on 2022-05-01 17:07:27