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