Can any one explain me the below
a few constructs are not deduced contexts
Qualified type names. A type name like
Q<T>::X
will never be used to deduce a template parameterT
, for example.Nontype expressions that are not just a nontype parameter. A type name like
S<I+1>
will never be used to deduceI
, for example. Neither willT
be deduced by matching against a parameter of typeint(&)[sizeof(S<T>)]
.These limitations should come as no surprise because the deduction would, in general, not be unique (or even finite).
Why the T
in Q<T>::X
can't be deduced ? and why int(&)[sizeof(S<T>)]
called as a Nontype expression, even it has the type parameter in it.
contents were taken from :"C++ Templates The Complete Guide"
EX(from book)
template <int N>
class X {
public:
typedef int I;
void f(int) {}
};
template<int N>
void fppm(void (X<N>::*p)(X<N>::I));
int main()
{
fppm(&X<33>::f); // fine: N deduced to be 33
}
In the function template
fppm()
, the subconstructX<N>::I
is a nondeduced context.
Edit
I've read some answers which trigger me an another other question
template<typename T> void(T p1,typename foo<T>::subtype P2)
in the above deceleration if
foo<T>
is not deduced which subtype the compiler will choose forp2
?
EDIT2
i am trying to below
#include <iostream>
#include <cstring>
template <typename T>
struct foo{
using type=int;
void print(type a){
std::cout<<a<<"\n";
}
};
template<>
struct foo<float>{
using type=float;
void print(type a){
std::cout<<a<<"\n";
}
};
template<typename T2>
void test(foo<T2> o,typename foo<T2>::type p){
o.print(p);
}
int main(){
foo<float> o;
test(o,1.2);
}
It's silly but I wonder what is the type of the T2 after test(o,1.2)