Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Turning off template parameter deduction

Reply
Thread Tools

Turning off template parameter deduction

 
 
KD
Guest
Posts: n/a
 
      06-19-2008
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?
 
Reply With Quote
 
 
 
 
Fei Liu
Guest
Posts: n/a
 
      06-19-2008
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
 
Reply With Quote
 
 
 
 
gpderetta
Guest
Posts: n/a
 
      06-19-2008
On Jun 19, 3:51*pm, Fei Liu <(E-Mail Removed)> 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



 
Reply With Quote
 
KD
Guest
Posts: n/a
 
      06-19-2008
On Jun 19, 11:08 am, gpderetta <(E-Mail Removed)> wrote:
> On Jun 19, 3:51 pm, Fei Liu <(E-Mail Removed)> 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.
 
Reply With Quote
 
Road.Tang
Guest
Posts: n/a
 
      06-20-2008
On Jun 19, 10:08 pm, gpderetta <(E-Mail Removed)> wrote:
> On Jun 19, 3:51 pm, Fei Liu <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
function template type deduction from default parameter tobias.loew@steag.com C++ 2 01-08-2013 07:53 AM
function pointer as template parameter + type deduction er C++ 5 06-09-2009 02:04 AM
template class instantiate without template parameter, automatic type deduction Fei Liu C++ 4 10-26-2007 02:39 PM
template class instantiate without template parameter, automatictype deduction Fei Liu C++ 0 10-25-2007 08:12 PM
Template parameter Deduction Neelesh C++ 4 11-10-2005 12:52 PM



Advertisments