Thread: Error while specifying return type in template

  1. #1
    Registered User
    Join Date
    Feb 2020
    Posts
    1

    Error while specifying return type in template

    [INDENT][FONT=arial narrow]Hi Alli, Can anyone please help me to understand why the code below gives a compilation error. The error message is posted below the code. Thanks in Advance
    Code:
    #include <iostream>
    
    template < typename T1, typename T2 > auto max(T1 a, T2 b)
    {
      return b < a ? a : b;
    }
    
    template < typename RT, typename T1, typename T2 > RT max(T1 a, T2 b)
    {
      return b < a ? a : b;
    }
    
    int main(int, char **)
    {
      std::cout <<::max < int >(4, 7.2);
    }

    Code:
    
    
    Code:
    a.cpp: In function 'int main(int, char**)': 
    
                                   a.cpp:16:32: error: call of overloaded 'max<int>(int, double)' is ambiguous 
    std::cout << ::max<int>(4, 7.2);
    
     a.cpp:4:6: note: candidate: 'auto max(T1, T2) [with T1 = int; T2 = double]'                                              
    auto max (T1 a, T2 b) 
    
    a.cpp:9:4: note: candidate: 'RT max(T1, T2) [with RT = int; T1 = int; T2 = double]'                                      
    RT max (T1 a, T2 b)
    The code is compiled with GCC, C++14

    Last edited by Salem; 02-05-2020 at 02:27 PM. Reason: fixed some of the crayola

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The compiler is telling you that the overload is (or at least can be) ambiguous. I would delete the first max function template as it looks wrong: you declared the return type as auto, but in one branch the return type could be resolved to T1 whereas in the other it could be resolved to T2. This should be an error unless T1 and T2 are the same type.

    EDIT: oh, come to think of it the expression must be of some type, so presumably there are implicit conversion rules involved when the second and third subexpressions differ in type, but if even I cannot recall what they are off-hand, relying on them is likely to be a bad idea.
    Last edited by laserlight; 02-05-2020 at 03:28 AM.
    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. Replies: 24
    Last Post: 04-19-2012, 04:51 AM
  2. Depending on Template Method Return Type
    By M.Richard Tober in forum C++ Programming
    Replies: 4
    Last Post: 06-28-2011, 09:34 PM
  3. Thoughts on return type from template container classes.
    By Subsonics in forum C++ Programming
    Replies: 17
    Last Post: 10-17-2010, 03:10 AM
  4. error when returning template type~
    By black in forum C++ Programming
    Replies: 5
    Last Post: 06-01-2004, 01:33 AM
  5. Constructors now allowed a return type: Mystery Error
    By wbeasl in forum C++ Programming
    Replies: 3
    Last Post: 04-01-2004, 02:33 PM

Tags for this Thread