Thread: Proper usage of templates

  1. #31
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92
    That package was missing a couple files Code::Blocks needs so I downloaded the MinGW installer 5.1.4 and when to Settings -> Compiler and Debugger ... Clicked the Toolchain tab and altered the MinGW install path to C:\MinGW

    I get the warnings and the newest version has none.

    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 static_cast<T>(sqrt(c));
    }
    
    template<class T> T pythagorean(const T &a, const T &b){
      return static_cast<T>(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;
    }
    Thanks!
    IDE + Complier: Code::Blocks w/ GCC
    PC: AMD Phenom x4 2.2Ghz w/ 8G RAM
    OSes: CentOS 6.2 x64 & Win 7 Ultimate x64

  2. #32
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92
    Is there a way to rename my squareroot to sqrt which calls the std libraries sqrt?
    IDE + Complier: Code::Blocks w/ GCC
    PC: AMD Phenom x4 2.2Ghz w/ 8G RAM
    OSes: CentOS 6.2 x64 & Win 7 Ultimate x64

  3. #33
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Is there a way to rename my squareroot to sqrt which calls the std libraries sqrt?

    The problem is C++ doesn't allow you to to overload/specialize a function that differs only in return type from another. There are a couple of workarounds:

    - Declare a set of pow functions in another namespace. It won't save you any typing, probably, but it's generally the best choice.

    - Simply use a different name. Ok, so this isn't really a workaround, but it's effective. You just have to get used to it, that's all. For example, when I'm writing generic code, I never use the % operator since I can't be sure what the underlying type is. Instead, I've defined these:

    Code:
    template < typename Type >
    Type mod( Type const& dividend, Type const& divisor, Type& remainder )
    {
    	return remainder = dividend % divisor;
    }
    
    template < typename Type >
    Type mod( Type const& dividend, Type const& divisor )
    {
    	Type 
    		remainder;
    	return mod( dividend, divisor, remainder );
    }
    
    float mod( float dividend, float divisor, float& remainder )
    {
    	return remainder = fmod( dividend, divisor );
    }
    
    double mod( double dividend, double divisor, double& remainder )
    {
    	return remainder = fmod( dividend, divisor );
    }
    
    int main( void )
    {
    	cout << mod( 20, 3 ) << endl;
    	cout << mod( 20.0f, 3.5f ) << endl;
    	cout << mod( 20.0, 3.5 ) << endl;
    }
    Of course, if I'm defining a new class it doesn't matter if I choose to overload operator % or mod(A, B, C) - either works. It took a little getting used to, but now even if I'm using just plain ints I'll use mod instead of %, just out of habit.
    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;
    }

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