C++ Logo

std-proposals

Advanced search

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

From: organicoman <organicoman_at_[hidden]>
Date: Fri, 25 Mar 2022 15:09:33 +0400
Your proposal could be interesting. But why do you want to explicitly write the type, if you can deduce it?NadSent from my Galaxy
-------- Original message --------From: Frederick Virchanza Gotham via Std-Proposals <std-proposals_at_[hidden]> Date: 3/25/22 2:47 PM (GMT+04:00) To: std-proposals_at_[hidden] Cc: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]> Subject: [std-proposals] Explicit keyword for template parameters 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 theprogrammer to have to explicitly specify the type, as follows: Func<ostream>(std::cout);In order to impose this restriction, I have to rewrite my templatefunction 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 disableargument-based template parameter deduction, as follows: template<explicit class T> void Func(T &arg) { /* Do Something */ }-- Std-Proposals mailing listStd-Proposals_at_[hidden]://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2022-03-25 11:09:43