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