a template parameter can be used in another template parameter that follows it this way :
template<typename T, T N>
struct s
{
};
But is it possible to reference "T" if it is declared after "N" ?
This does not work :
template<T N, typename T>
struct s
{
};
Can we help the compiler by pre-declaring "T" or doing anything else ?
Thanks by advance.
EDIT : as the first two replies were asking "why are you willing to do that ?" I'll explain the goal :
I would like to make the compiler infer the type "T" in order to make the use of templated classes easier.
For example :
template<typename T, T A, T B>
struct sum
{
static T const value = A + B;
};
This template can be used this way :
sum<int, 1, 2>::value
But it would be better if it could be used this way :
sum<1, 2>::value
Technically it's should be possible because the compiler knows the types of "1" and "2" : "int", and in fact it uses these informations to find the best overload for a function. So by declaring the template this way :
template<T A, T B, typename T>
struct sum
{
static T const value = A + B;
};
the compiler could use its capability to infer the last parameter from the informations provided by the first and the second one, and then find the best template to instantiate.
Like others say - No this isn't possible, the compiler can't infer the type of T
from the non-type template arguments (in the case of functions, it infers types from the function arguments):
14.8.2.4/12
:
A template type argument cannot be deduced from the type of a non-type template-argument.
In any case, no deduction will be made for the arguments of a class template anyway. An example for a function template might be
template<int> struct having_int { };
template<typename T, T i> void f(having_int<i>);
int main() { having_int<0> h; f(h); }
In this case, T
won't be deduced as int
- you have to explicitly specify it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With