Thread: Error while specifying return type in template

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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