red floyd
11/26/2008 5:00:00 PM
On Nov 26, 3:31 am, olekk <sashaks...@gmail.com> wrote:
> hweek...@yahoo.com wrote:
> > Hi,
>
> > I am trying to experiment with expression templates and come across
> > this issue of implicit cast for the arguments of templated functions,
> > below is the code and compile error. Why is the compiler not able to
> > cast? Thanks.
>
> > 1 #include <iostream>
> > 2
> > 3 template<class U>
> > 4 struct T {
> > 5 T(U i) : b(i) { }
> > 6 U b;
> > 7 };
> > 8 // ---------------------------
> > 9 template<class A>
> > 10 void print(T<A>& t,T<A>& y) {
> > 11 std::cout<<__LINE__<<" "<<t.b<<std::endl;
> > 12 }
> > 13 // ---------------------------
> > 14 int main ( ) {
> > 15
> > 16 T<int> t1(2),t2(4);
> > 17
> > 18 print(t1,3);
> > 19 }
> > 20 // ---------------------------
>
> > compile error
> > > g++ -Wall u.C
> > u.C: In function int main() :
> > u.C:18: error: no matching function for call to print(T<int>&, int)
>
> It's because the arguments for a class template can not be deduced
> from the arguments of its constructor.
>
And even if they could be deduced (which they can't), you wouldn't
match anyways, because you wouldn't be able to bind the temporary to
the reference for the second param.