Turning off template parameter deduction

K

KD

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?
 
F

Fei Liu

KD said:
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
 
G

gpderetta

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,
 
K

KD

To implement something similar to standard C++ casts for example.




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,

Thank you. This seems to work for me, and is cleaner than my previous
solution.
 
R

Road.Tang

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.
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top