templates question (new standard vs. older)

This is a discussion on templates question (new standard vs. older) within the C++ Programming forums, part of the General Programming Boards category; I was just playing around with templates, and I encountered the following problem. using a new standard headers i had ...

  1. #1
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279

    templates question (new standard vs. older)

    I was just playing around with templates, and I encountered the following problem.

    using a new standard headers i had the following:

    Code:
    #include<iostream>
    #include"test.h"
    using namespace std;
    
    int main()
    {
            char ch;
    
            int x = 10;
            int y = 15;
    
            cout << "x before: " << x << "\t y before: " << y << endl;
            swap(x, y);
    
            cout << "x after: " << x  << "\t y after: " << y << endl;
    
            cin.get(ch);
    
            return 0;
    }
    
    
    
    AND THE TEMPLATE HEADER CLASS IS AS FOLLOWING:
    
    template<class T>
    void swap(T& one, T& two)
    {
            T temp;
    
            temp = one;
            one = two;
            two = temp;
    
            return;
    }
    I get the following error:

    c:\documents and settings\mm59\desktop\cis 113\test4\test4.cpp(14) : error C2667: 'swap' : none of 2 overload have a best conversion
    c:\documents and settings\mm59\desktop\cis 113\test4\test4.cpp(14) : error C2668: 'swap' : ambiguous call to overloaded function


    However, when I use the older type with the 'using' directive, as following then there's no errors and everything works fine:

    #include<iostream.h>
    #include"test.h"


    Can anyone explain to me why such thing occurs??? I used Borland 5.0 and Microsoft Visual C++ 6.0 and same results on both.....hmmm....

    Maybe I'm doing something wrong when it comes to the new standard and templates???

    Thanks for all info in advance...


    matheo917

  2. #2
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    gcc tells all:
    Code:
    Mon 12-09-2002 22:45:09.23
    C:\WINDOWS\Desktop>g++ dum.cpp -o dum.exe -Wall
    dum.cpp: In function `int main()':
    dum.cpp:27: call of overloaded `swap(int&, int&)' is ambiguous
    dum.cpp:5: candidates are: void swap(T&, T&) [with T = int]
    C:/MINGW/include/c++/3.2/bits/stl_algobase.h:121: void std::swap(_Tp&, _Tp&) [with _Tp = int]
    hello, internet!

  3. #3
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279

    hmmm

    so let me see if i'm reading this right...

    if it comes to the new standard the linker (b/c it compiles fine) states that when it comes to only integers then the function itself should be defined for just integers instead of of template T.

    hmmm....i can't get the clear picture here, if my memory serves me correctly then the 'swap' function is defined only for those data types that call the function....

    can you explain in more detail please?

    thanx

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I'm a bit confused too, especially since I got it to compile just fine (and have used swap without errors many times before).

  5. #5
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    When I compiled I got the same two errors, but what I did was that I changed the name of the swap function, and it compiled fine.
    none...

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Your problem is that you are exactly redefining a template that is already part of the Standard C++ Library.....

    although you arent including the header for std::swap, I imagine that it is used in the iostream header somewhere, so when you drag all of namespace std into your code (with that using statement), you are including std::swap in your list of possible template functions...

    Best way forward;
    [list=1][*]Remove "using namespace std" and use namespace qualifiers[*]Use a "using" statement for each object you are using[*]Best of all - dont bother to redefine a function in the STL[/list=1]

  7. #7
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    that's what I was thinking of,
    Overriding a template with another template in the same file, causes a compiler error, but overriding with a normal function is OK, that's why if we define swap as a normal function(not a template) we don't get that problem.

    Thank you Fordy, for the answer.
    none...

  8. #8
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    thanx to all

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class Templates question
    By Swerve in forum C++ Programming
    Replies: 8
    Last Post: 03-29-2009, 01:42 PM
  2. Stack Question With Templates
    By Anonymous Freak in forum C++ Programming
    Replies: 6
    Last Post: 02-09-2003, 12:18 PM
  3. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 10:50 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21