![]() |
Turning off template parameter deduction
I have a template function and I'm looking for a way to force the
caller to specify the template parameters. In other words, I would like to turn off template parameter deduction. For example, template <class T> void foo(T t) { ... } foo(123); // I want this to fail. foo<int>(123); // I want this to succeed. I was hoping that the explicit keyword would work, but it didn't. So far, the only half working hack that I came up with is having two parameters S and T, where S is not used so it must be specified. In the method, I then do static checks that T is convertible to S, but it doesn't work in all cases and it adds some complexity to the code. Any ideas? |
Re: Turning off template parameter deduction
KD wrote:
> I have a template function and I'm looking for a way to force the > caller to specify the template parameters. In other words, I would > like to turn off template parameter deduction. For example, > > template <class T> > void foo(T t) > { > ... > } > > foo(123); // I want this to fail. > foo<int>(123); // I want this to succeed. > > I was hoping that the explicit keyword would work, but it didn't. So > far, the only half working hack that I came up with is having two > parameters S and T, where S is not used so it must be specified. In > the method, I then do static checks that T is convertible to S, but it > doesn't work in all cases and it adds some complexity to the code. > > Any ideas? the more important question to ask is: why would you want to do something like that? Although I believe there are ways to do what you wanted but I don't think there is a generic solution. F |
Re: Turning off template parameter deduction
On Jun 19, 3:51*pm, Fei Liu <fei....@gmail.com> wrote:
> KD wrote: > > I have a template function and I'm looking for a way to force the > > caller to specify the template parameters. *In other words, I would > > like to turn off template parameter deduction. *For example, > > > template <class T> > > void foo(T t) > > { > > * * ... > > } > > > foo(123); *// I want this to fail. > > foo<int>(123); // I want this to succeed. > > > I was hoping that the explicit keyword would work, but it didn't. *So > > far, the only half working hack that I came up with is having two > > parameters S and T, where S is not used so it must be specified. *In > > the method, I then do static checks that T is convertible to S, but it > > doesn't work in all cases and it adds some complexity to the code. > > > Any ideas? > > the more important question to ask is: why would you want to do > something like that? To implement something similar to standard C++ casts for example. > > Although I believe there are ways to do what you wanted but I don't > think there is a generic solution. > Sure that there is: just put T in a non deducible context: template<typename T> struct identity { typedef T type; }; template<typename T> void do_not_deduce(typename identity<T>::type x); HTH, -- gpd |
Re: Turning off template parameter deduction
On Jun 19, 11:08 am, gpderetta <gpdere...@gmail.com> wrote:
> On Jun 19, 3:51 pm, Fei Liu <fei....@gmail.com> wrote: > > > > > KD wrote: > > > I have a template function and I'm looking for a way to force the > > > caller to specify the template parameters. In other words, I would > > > like to turn off template parameter deduction. For example, > > > > template <class T> > > > void foo(T t) > > > { > > > ... > > > } > > > > foo(123); // I want this to fail. > > > foo<int>(123); // I want this to succeed. > > > > I was hoping that the explicit keyword would work, but it didn't. So > > > far, the only half working hack that I came up with is having two > > > parameters S and T, where S is not used so it must be specified. In > > > the method, I then do static checks that T is convertible to S, but it > > > doesn't work in all cases and it adds some complexity to the code. > > > > Any ideas? > > > the more important question to ask is: why would you want to do > > something like that? > > To implement something similar to standard C++ casts for example. > > > > > Although I believe there are ways to do what you wanted but I don't > > think there is a generic solution. > > Sure that there is: just put T in a non deducible context: > > template<typename T> struct identity { typedef T type; }; > > template<typename T> > void do_not_deduce(typename identity<T>::type x); > > HTH, > > -- > gpd Thank you. This seems to work for me, and is cleaner than my previous solution. |
Re: Turning off template parameter deduction
On Jun 19, 10:08 pm, gpderetta <gpdere...@gmail.com> wrote:
> On Jun 19, 3:51 pm, Fei Liu <fei....@gmail.com> wrote: > > > > > KD wrote: > > > I have a template function and I'm looking for a way to force the > > > caller to specify the template parameters. In other words, I would > > > like to turn off template parameter deduction. For example, > > > > template <class T> > > > void foo(T t) > > > { > > > ... > > > } > > > > foo(123); // I want this to fail. > > > foo<int>(123); // I want this to succeed. > > > > I was hoping that the explicit keyword would work, but it didn't. So > > > far, the only half working hack that I came up with is having two > > > parameters S and T, where S is not used so it must be specified. In > > > the method, I then do static checks that T is convertible to S, but it > > > doesn't work in all cases and it adds some complexity to the code. > > > > Any ideas? > > > the more important question to ask is: why would you want to do > > something like that? > > To implement something similar to standard C++ casts for example. > IMO, to implement cast like things doesn't need the trick. template <typename T, typename S> T& user_cast(S& s); T is result type, can't be deduced yet. > > > > Although I believe there are ways to do what you wanted but I don't > > think there is a generic solution. > > Sure that there is: just put T in a non deducible context: > > template<typename T> struct identity { typedef T type; }; > > template<typename T> > void do_not_deduce(typename identity<T>::type x); > aha , impressive trick. > HTH, > > -- > gpd |
| All times are GMT. The time now is 05:39 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.