Re: Overloading a template member function with a dependent name
On May 20, 5:21 pm, "Alf P. Steinbach /Usenet" <alf.p.steinbach
+use...@gmail.com> wrote:
* mlimber, on 20.05.2011 22:37:
I'm trying to overload a template member function with a dependent
name involved. The following does not work as I'd like:
class C
{
public:
template<class Iter>
void Foo( Iter )
{
std::cout<< "Normal\n";
}
template<class T, std::size_t N>
void Foo( typename std::tr1::array<T,N>::iterator )
{
std::cout<< "Special\n";
}
};
int main()
{
C c;
std::tr1::array<int,10> a1;
c.Foo( a1.begin() ); // Doesn't print "Special"!
}
How can I get that last line in main() to invoke the special C::Foo()?
Why there is a problem:
<code>
template< class Type >
struct Foo
{
typedef int T;
};
template< class Type >
void foo( typename Type::T ) {}
int main()
{
foo( 666 ); // !Nope
}
</code>
This does not compile because the compiler cannot deduce the template par=
ameter.
It cannot because it would be an unbounded reverse lookup: given the actu=
al
argument type `int`, find the type `Type` that has a member typedef of `T=
` with
type `int`. There can be zillions or none of such types.
There are techniques that sometimes can be used to still do something /li=
ke/ you
seem to be aiming for.
One crucial question for applicability of such techniques here is N: do y=
ou need
to know it? In that case I don't know any solution other than changing th=
e
design. I don't think it can be inferred from the iterator type, at all.
Thanks, Alf. I don't need N (it would be sugar to have it, but since
I'm actually passing in begin and end, I can calculate it). So do
tell!
Cheers! --M