Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Turning off template parameter deduction (http://www.velocityreviews.com/forums/t621169-turning-off-template-parameter-deduction.html)

KD 06-19-2008 01:41 PM

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?

Fei Liu 06-19-2008 01:51 PM

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

gpderetta 06-19-2008 02:08 PM

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




KD 06-19-2008 02:55 PM

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.

Road.Tang 06-20-2008 08:35 AM

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.