[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

implicit cast in templated function

hweekuan

11/26/2008 10:09:00 AM

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)’

----------
g++ --version
i686-apple-darwin9-g++-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5465)
Copyright (C) 2005 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There
is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.


3 Answers

olekk

11/26/2008 11:32:00 AM

0



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.

Regards

red floyd

11/26/2008 5:00:00 PM

0

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.

Hendrik Schober

11/26/2008 5:55:00 PM

0

hweekuan@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.

There's two issues. One is that you're trying to bind a
literal to a non-const reference. This doesn't work even
with non-template functions. The other is that template
argument deduction doesn't work the way you think.
If you make the second parameter 'const' and explicitly
specify the template argument ('print<int>(t1,3)'), then
Comeau compiles the code without complaints.

HTH,

Schobi