implicit cast in templated function

Discussion in 'C++' started by hweekuan@yahoo.com, Nov 26, 2008.

  1. Guest

    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.
    , Nov 26, 2008
    #1
    1. Advertising

  2. olekk Guest

    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
    olekk, Nov 26, 2008
    #2
    1. Advertising

  3. red floyd Guest

    On Nov 26, 3:31 am, olekk <> wrote:
    > 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.
    red floyd, Nov 26, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RA Scheltema
    Replies:
    3
    Views:
    380
    RA Scheltema
    Jan 6, 2004
  2. Marijn
    Replies:
    5
    Views:
    442
    Marijn
    Feb 13, 2004
  3. Amadeus W. M.
    Replies:
    2
    Views:
    383
    Amadeus W. M.
    Jul 4, 2006
  4. chhenning
    Replies:
    5
    Views:
    354
    chhenning
    Feb 13, 2008
  5. DeMarcus
    Replies:
    9
    Views:
    321
    DeMarcus
    Mar 4, 2010
Loading...

Share This Page