Thread: function templates

  1. #1
    Registered User thriller500's Avatar
    Join Date
    Oct 2011
    Posts
    25

    function templates

    Code:
    #include<iostream>
    using namespace std;
    template <class t,class q>
    t max(t a,q b)
    {
        return ((a>b)?a:b);
        
    }
    
    
    int main()
    {
       int x=7;
       double y=6;
       double z;
       z=max(x,y);
       cout<<z;
       system("Pause"); 
        
        
        
        
        
        
    }



    Error:
    In function `t max(t, q) [with t = int, q = double]':
    line 15 : instantiated from here..




    this code worked fine in code blocks.. i do not understand the problem in dev c++ now...

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    Try changing the name of the max() function, it could be already defined (e.g. as a macro) in one of the standard header files.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Fix the types to follow your own type signature. It says right in the warning (full text given here):
    In function 't max(t, q) [with t = int, q = double]':
    t.cpp:17: instantiated from here
    Line 6: warning: converting to 'int' from 'double'

    So t will be the result type. In this case, it would be int, since you passed in an int first, but in main() you assign the result to z, a double.

    It's also a bad idea to convert to int from double since the compiler will warn about it every time without a cast. It's not a trivial conversion.
    Last edited by whiteflags; 07-04-2012 at 02:48 AM.

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    A part of this warning comes from the nature of the "conditional operator"; both "results" must be the same type so the compiler "widens" the `int' result to a `double' result and then must "narrow" the `double' result back to a `int' for the return value. Most compilers will warn about this without explicit conversion.

    For vanilla C++98, it would be somewhat better in this case to use one type from the template for both parameters; more compilers would give a more specific warning and allow clients to choose the return type as appropriate by casting the parameters to a type they feel best suites their purpose.

    For a more complex C++98, you could use the rules of implicit conversion according to the standard to "widen" the result type to whichever parameter is "widest".

    For a C++11 approach you can use the "alternate function syntax" to let the compiler "widen" the result for you allowing you to have to different types in the function signature and insert explicit casts to prevent such warnings; this approach would also work with classes that match certain criteria.

    Code:
    template
    <
        typename FLHS
      , typename FRHS
    >
    auto max
    (
        FLHS fLHS
      , FRHS fRHS
    ) -> decltype((fLHS > fRHS) ? fLHS : fRHS)
    {
        typedef decltype((fLHS > fRHS) ? fLHS : fRHS) UResult;
        return((fLHS > fRHS) ? static_cast<UResult>(fLHS) : static_cast<UResult>(fRHS));
    }
    Soma

  5. #5
    Registered User thriller500's Avatar
    Join Date
    Oct 2011
    Posts
    25
    I am not worried about the warnings..
    i just want the code to compile....

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Disable "treat warnings as errors"?

    *shrug*

    Without more context we have no way to help you more than we have already.

    Soma

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    As already suggested, the most likely explanation is that the function "max" already exists, as a function or a macro, in standard system headers. Perhaps Code::Blocks, with one version of g++, does not include the "max" header (cmath?) inside iostream, while Dev-C++ does. It's quite possible to have standard headers including each other differently with different compiler versions. I would rename the function to "maximum" or something and see what happens.

    By the way, as an example of system headers including each other: compiling your code with my compiler, I get an error about system() not being defined, because system() is actually from <cstdlib>. On your compiler, presumably <iostream> includes <cstdlib>, but mine does not. Your code should really be including <cstdlib> since you're using a function from that header.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating Function Templates
    By marQade in forum C++ Programming
    Replies: 26
    Last Post: 12-03-2007, 04:07 PM
  2. friend function in templates
    By arjunajay in forum C++ Programming
    Replies: 0
    Last Post: 01-22-2006, 04:32 AM
  3. Templates - Function Partial Specialization?
    By Cat in forum C++ Programming
    Replies: 2
    Last Post: 05-28-2003, 07:53 AM
  4. Function Templates
    By Trauts in forum C++ Programming
    Replies: 5
    Last Post: 05-02-2003, 05:55 PM
  5. Clarification of Function Templates
    By biosx in forum C++ Programming
    Replies: 2
    Last Post: 02-20-2002, 11:42 AM