Templates

Discussion in 'C++' started by g18c@hotmail.com, Dec 6, 2005.

  1. Guest

    Hi, i am trying to overload a function with a number of specific
    template functions without much sucess.

    template<typename T> void TmplFunc(T& t)
    {
    }

    template<typename T> void TmplFunc(list<T>& t)
    {
    list<T>::const_iterator fi = t.begin();
    }

    void main()
    {
    list<int> myList;
    TmplFunc(myList);
    }

    On compilation i get an error saying none of the two overloads have a
    best conversion. I appreciate for "void TmplFunc(T& t)" T could also be
    a list... is there any way i can have the "void TmplFunc(list<T>& t)"
    get called without resorting to calling the specific template with
    "TmplFunc<list<int> >(myList)"? Or even better, i would like a template
    function that can deal with any container object which has iterators!

    Thanks in advance,

    Chris
     
    , Dec 6, 2005
    #1
    1. Advertising

  2. wrote:
    > Hi, i am trying to overload a function with a number of specific
    > template functions without much sucess.
    >
    > template<typename T> void TmplFunc(T& t)
    > {
    > }
    >
    > template<typename T> void TmplFunc(list<T>& t)
    > {
    > list<T>::const_iterator fi = t.begin();
    > }


    typename list<T>::const_iterator fi = t.begin();

    > void main()


    main must return int.

    > {
    > list<int> myList;
    > TmplFunc(myList);
    > }
    >



    Interestingly enough, my previous post was also for the same type of
    question!!
     
    Neelesh Bodas, Dec 6, 2005
    #2
    1. Advertising

  3. On 6 Dec 2005 07:38:03 -0800, "" <>
    wrote:

    >Hi, i am trying to overload a function with a number of specific
    >template functions without much sucess.
    >
    >template<typename T> void TmplFunc(T& t)
    >{
    >}
    >
    >template<typename T> void TmplFunc(list<T>& t)
    >{
    > list<T>::const_iterator fi = t.begin();
    >}
    >
    >void main()


    This should be:
    int main()

    >{
    > list<int> myList;
    > TmplFunc(myList);
    >}
    >
    >On compilation i get an error saying none of the two overloads have a
    >best conversion. I appreciate for "void TmplFunc(T& t)" T could also be
    >a list... is there any way i can have the "void TmplFunc(list<T>& t)"
    >get called without resorting to calling the specific template with
    >"TmplFunc<list<int> >(myList)"? Or even better, i would like a template
    >function that can deal with any container object which has iterators!


    I don't know, this works for me:

    // test_template.cpp
    #include <iostream>
    #include <ostream>
    #include <list>

    template<typename T>
    void TmplFunc(T& t)
    {
    }

    template<typename T>
    void TmplFunc(std::list<T>& t)
    {
    typename std::list<T>::const_iterator fi = t.begin();
    std::cout << "Called TmplFunc(list<T>&)" << std::endl;
    }

    int main()
    {
    std::list<int> myList;
    TmplFunc(myList);
    }

    --
    Bob Hairgrove
     
    Bob Hairgrove, Dec 6, 2005
    #3
  4. Chris Morley Guest

    Not sure where my other post has gone so sorry if this appears twice... What
    compiler is that with bob?
     
    Chris Morley, Dec 6, 2005
    #4
  5. On Tue, 6 Dec 2005 16:47:42 -0000, "Chris Morley" <>
    wrote:

    >Not sure where my other post has gone so sorry if this appears twice... What
    >compiler is that with bob?


    I tried it with MSVC++ 7.1 and Borland 5.5.1, but it would probably
    work with any modern compiler.

    --
    Bob Hairgrove
     
    Bob Hairgrove, Dec 6, 2005
    #5
  6. Guest

    Thanks for the reply, what compiler are you using? Im using Visual
    Studio 2002
     
    , Dec 6, 2005
    #6
  7. Chris Morley Guest

    > I tried it with MSVC++ 7.1 and Borland 5.5.1, but it would probably
    > work with any modern compiler.


    MSVC++7.0 doesn't seem to be modern because it doesn't work for me!! Surely
    ppl have had these problems before? I cant use 7.1 because other people in
    the team use MSVC++7.0 too. This is extremely frustrating!
     
    Chris Morley, Dec 6, 2005
    #7
  8. On 6 Dec 2005 09:02:58 -0800, "" <>
    wrote:

    >Thanks for the reply, what compiler are you using? Im using Visual
    >Studio 2002


    MSVC++ 7.1 and Borland 5.5.1 both compile it without errors.

    --
    Bob Hairgrove
     
    Bob Hairgrove, Dec 6, 2005
    #8
  9. Ben Pope Guest

    Chris Morley wrote:
    >> I tried it with MSVC++ 7.1 and Borland 5.5.1, but it would probably
    >> work with any modern compiler.

    >
    > MSVC++7.0 doesn't seem to be modern because it doesn't work for me!!


    No, it wouldn't be classed as modern with regards templates.

    Ben Pope
    --
    I'm not just a number. To many, I'm known as a string...
     
    Ben Pope, Dec 6, 2005
    #9
    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. Fred
    Replies:
    1
    Views:
    635
    Neredbojias
    Sep 26, 2005
  2. John Harrison

    using templates in templates

    John Harrison, Jul 31, 2003, in forum: C++
    Replies:
    8
    Views:
    402
    Torsten Curdt
    Jul 31, 2003
  3. JKop
    Replies:
    3
    Views:
    520
  4. Tom McCallum

    Templates within templates

    Tom McCallum, Aug 4, 2004, in forum: C++
    Replies:
    2
    Views:
    381
    tom_usenet
    Aug 4, 2004
  5. recover
    Replies:
    2
    Views:
    855
    recover
    Jul 25, 2006
Loading...

Share This Page