Thread: Call to templated function is ambiguous when using float

  1. #1
    Registered User
    Join Date
    Sep 2019
    Posts
    15

    Call to templated function is ambiguous when using float

    Hello, I have this function:

    Code:
    template <typename T>
    void swap (T& arg1, T& arg2) {
       T& tmp = arg1;
       arg1  = arg2;
       arg2  = tmp;
    }
    When I make a call it passing float arguments, like here:

    Code:
    if (tmin > tmax) swap(tmin, tmax);
    , I receive:

    Code:
    /media/34GB/demos/asmfrt/Bbox.h|29|error: call of overloaded ‘swap(float&, float&)’ is ambiguous|
    Code:
    /media/34GB/demos/asmfrt/Miscellaneous.h|44|note: candidates are: void swap(T&, T&) [with T = float]|
    How can this be treated? Thanks in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    A few options come to mind:

    A. Declare your function template within your own namespace and call it qualified with the namespace name.

    B. Be more careful with your use of using directives and using declarations. In this case it means avoiding using namespace std and/or using std::swap in contexts such that it will allow swap from the std namespace to be called without qualification.

    C. Rename your function template so that it won't conflict with swap from the std namespace.

    Of course, in production code you should just use std::swap for such types.
    Last edited by laserlight; 09-03-2020 at 07:56 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by colt View Post
    Hello, I have this function:

    Code:
    template <typename T>
    void swap (T& arg1, T& arg2) {
       T& tmp = arg1;
       arg1  = arg2;
       arg2  = tmp;
    }
    Whilst not related to your actual problem, the temporary should be a T, not a T &.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  4. #4
    Registered User
    Join Date
    Sep 2019
    Posts
    15
    Quote Originally Posted by laserlight View Post
    A few options come to mind:

    A. Declare your function template within your own namespace and call it qualified with the namespace name.

    B. Be more careful with your use of using directives and using declarations. In this case it means avoiding using namespace std and/or using std::swap in contexts such that it will allow swap from the std namespace to be called without qualification.

    C. Rename your function template so that it won't conflict with swap from the std namespace.

    Of course, in production code you should just use std::swap for such types.
    I solved it using namespace, but I am in doubt from where the std swap was coming from. The only thing I was calling, was #include <iostream>


    Quote Originally Posted by Malcolm McLean View Post
    Whilst not related to your actual problem, the temporary should be a T, not a T &.
    But it is not bad for the performance if the type is of something "big"

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by colt
    The only thing I was calling, was #include <iostream>
    However, that header could indirectly include a header that defines std::swap.

    Quote Originally Posted by colt
    But it is not bad for the performance if the type is of something "big"
    The problem is that it is simply incorrect if you're trying to implement a swap. If you make tmp a reference and initialise it to refer to arg1 instead of making it a copy of arg1, then when you assign arg2 to arg1, tmp will also be changed (since it is an alias), so when you assign tmp to arg2, it will have no net effect: you're just assigning a copy of arg2 to arg2, so what you end up with is both arg1 and arg2 having the original value of arg2. That's not a swap.

    You should be aware that since the advent of C++11, a generic swap like this (including std::swap) is likely to be implemented using move semantics:
    Code:
    T tmp = std::move(arg1);
    arg1 = std::move(arg2);
    arg2 = std::move(tmp);
    If moving is more efficient than copying for type T, this would then address the performance issue that you mentioned.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ambiguous function call
    By Elysia in forum C++ Programming
    Replies: 12
    Last Post: 05-08-2008, 05:31 AM
  2. Ambiguous call to function
    By Kayoss in forum C++ Programming
    Replies: 6
    Last Post: 04-06-2006, 11:47 AM
  3. Ambiguous call to overloaded function and more
    By Baard in forum C++ Programming
    Replies: 2
    Last Post: 02-29-2004, 01:41 PM
  4. ambiguous call to overloaded function
    By LurPak in forum C++ Programming
    Replies: 2
    Last Post: 09-05-2003, 03:37 AM
  5. ambiguous function call error
    By PorkyChop in forum C++ Programming
    Replies: 4
    Last Post: 12-21-2002, 09:59 PM

Tags for this Thread