Thread: Proper usage of templates

  1. #1
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92

    Proper usage of templates

    Before I get too far ahead of myself, is this the proper usage of templates?

    Code:
    #include <iostream>
    
    using namespace std;
    
    template<class T> T pythagorean(const T &a, const T &b){
      return a * a + b * b;
    }
    
    int main(){
    
      int a = 2, b = 4;
      double c = 10, d = 3;
    
      cout << pythagorean(a, b) << endl << pythagorean(c, d) << endl;
    
      return 0;
    }
    TIA!

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Are you asking if it compiles? You should not be asking here without trying yourself first.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It appears to be correct. Why do you ask?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92
    I know it compiles and I know it works, I just want to make sure I am using best practices.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Before I get too far ahead of myself, is this the proper usage of templates?

    Yep, looks good. I'd recommend changing the name to dot_product or inner_product, though.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by Sebastiani View Post
    >> Before I get too far ahead of myself, is this the proper usage of templates?

    Yep, looks good. I'd recommend changing the name to dot_product or inner_product, though.
    or return the square root of what's there now
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  7. #7
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92
    Yeah, that was a mistake on my part ... it returns c squared, instead of c.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> or return the square root of what's there now

    You'd probably want to reuse it, though. Just have a separate function for that.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Yeah, that was a mistake on my part ... it returns c squared, instead of c.

    No worries, like I said a dot_product function comes in handy.

    Code:
    template < Typename T >
    inline T vector_magnitude( T const& lhs, T const& rhs )
    {
    	return sqrt( dot_product( lhs, rhs ) );
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #10
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92
    Code:
    #include <iostream>
    #include <string>
    #include <math.h>
    
    using namespace std;
    
    template<class T> T inner_product(const T &a, const T &b){
      return a * a + b * b;
    }
    
    template<class T> T squareroot(const T &c){
      return sqrt(c);
    }
    
    template<class T> T pythagorean(const T &a, const T &b){
      return squareroot(inner_product(a, b));
    }
    
    int main(){
    
      int a = 2, b = 4;
      double c = 10, d = 3;
      string string1 = "This is a test.";
      string string2 = "This is another test with more characters.";
    
      cout << pythagorean(a, b)
           << endl << pythagorean(c, d)
           << endl << pythagorean(string1.size(), string2.size());
    
      return 0;
    }

  11. #11
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92
    Quote Originally Posted by Sebastiani View Post
    >> Yeah, that was a mistake on my part ... it returns c squared, instead of c.

    No worries, like I said a dot_product function comes in handy.

    Code:
    template < Typename T >
    inline T vector_magnitude( T const& lhs, T const& rhs )
    {
    	return sqrt( dot_product( lhs, rhs ) );
    }
    I've forgotten so much math I wouldn't know. :/

  12. #12
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Actually the squareroot function is producing warnings with these instantiations (sqrt returns a double, but your function returns T - which is an int in this case). I think you can use a static_cast to tell the compiler that you are aware of that and can live with it, or you'll need to think whether returning T is proper.

    Code:
    template<class T> T squareroot(const T &c){
      return static_cast<T>(sqrt(c)); //it's fine if the result is truncated
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  13. #13
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92
    What compiler are you using?

    I am using Code::Blocks with GNU GCC and I don't get any complaints, but I am sure you are right.

  14. #14
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> I've forgotten so much math I wouldn't know. :/

    I guess that's the good thing about programming. And I should probably point out that it might be more appropriately named unary_dot_product or such, as it's really just the dot_product of a vector with itself.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  15. #15
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92
    ## edit

    I see now ... sqrt doesn't support type int

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Net cpu usage of pthreads?!
    By mynickmynick in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2008, 07:59 AM
  2. Memory usage and memory leaks
    By vsanandan in forum C Programming
    Replies: 1
    Last Post: 05-03-2008, 05:45 AM
  3. Proper Usage of the delete Operator
    By thetinman in forum C++ Programming
    Replies: 7
    Last Post: 04-25-2007, 11:53 PM
  4. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2005, 12:22 AM