H
hweekuan
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
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.
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
u.C: In function ‘int main()’:g++ -Wall u.C
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.