Re: Difficulty declaring free function that takes parameter that
is a nested member of a class template
On 1/18/2012 7:58 PM, Travis Vitek wrote:
Can someone explain why the compiler is unable to deduce T when
invoking the function template `f' in the example below?
The answer to that is usually, "Because that is not one of the contexts
from which the compiler is required to deduce the template arguments".
> Is there some
way to get the compiler to see `f' without having to explicitly
specify the template argument type?
template<class T>
struct A {
enum B {
};
};
template<class T>
void f(typename A<T>::B a)
{
}
int main ()
{
A<int>::B a0 = A<int>::B();
f(a0); // don't want to explicitly specify f<int>(a0) here
}
I have been able to move the definition of `f' into A to get the
results I want, but I'd like to declare/define the function outside of
A if possible. Is there something wrong with my declaration of `f', or
is there some other way to do what I want?
Wrong? You'll have to define "wrong". A member of a template is not a
deducible context. The reason is rather benign, I think: there can be a
specialization (or partial, which means virtually unlimited number of
them) of your 'A' template which has no 'B' member, or a different 'B'
member, so the compiler would have to look through all specializations
of 'A' (the set of which is open at the time of compiling 'f(a0)', to
determine what 'T' is. That's asking too much, and thus the members of
template are not required to be a deducible context.
V
--
I do not respond to top-posted replies, please don't ask