Thread: HOw to convert to Template

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    18

    HOw to convert to Template

    Hey guys i'm just wondering if someone could help me convert this to a template function...

    Code:
    #include<iostream>
    #include<cmath>
    using namespace std;
    
    int main() 
    {	
    	 double  answer1 = 1, answer2 = 1, exponent = 1;
    
    
    	/* prompt the user */
    	cout << "This program calculates subtracts 2pow(n) from 3pow(n).\n\n";
    	cout << "Please enter a number (n): ";
    	cin >> exponent;
    	cout << "\n";
    
    	
    	answer1 = pow(3,exponent);
    	answer2 = pow(2,exponent);
    
    	cout<< answer1 - answer2 << endl;
    	
    	return 0;
    }

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You can't template main(). And doing a template pow() doesn't make a lot of sense (IMO).

    But...

    First step is to write the function itself as a function. Go through the function and make sure you aren't making assumptions based off of the type. Make sure you use generic operators like + * > etc.
    Change your types to a generic name
    add
    template<typename generic_name>

    do that and post what you've got and we can help you further

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    18
    Would this calculate the result at "compile time" f(n) = 3 power(n) - 2power(n)..

    Code:
    #include<iostream>
    using namespace std;
    
    
     template <class Foo>
       Foo Sub(Foo a) 
       {
    	 int  count = 1;
    	 double answer1 = 1, answer2 = 1;
    
    	for ( count = 1 ; count <= a ; count++){
    		 answer1 *= 3;
    			 answer2 *= 2;
    	}
         return answer1 - answer2;
    
       }
    
     int main()
     {
    	cout<<	Sub(3) << endl ;
    	return 0; 
     }

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    No, that calculation would be done at run-time.

    And in your templated function, you're assuming that a is an integer. That's the only way that your code makes sense.

    I really don't understand what you're trying to accomplish here. Calculating 3^n-2^n takes only a few lines of C or C++ code from beginning to end. There's no need for templates whatsoever. If you're just trying to practice template programming, I suggest implementing a class like vector.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    18
    I'm trying to write a single template class whose instantiation with Foo<n>::result computes f(n) = 3^n - 2 ^ n at COMPILE TIME! Its a hw problem due in 30 minutes..THe hint given is how does f(n+1) relate to f(n)

  6. #6
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Computations aren't done at compile time; compiling is. Have you talked to your prof or TA about this?

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    18
    No I haven't

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Quote Originally Posted by joshdick
    Computations aren't done at compile time; compiling is. Have you talked to your prof or TA about this?
    You can do computations at compile time. Template metaprogramming. This is doable. Strange to be doing this for a class, and too complicated to help you find an answer in 30 minutes which is already up. I would hope you have been taught some template metaprogramming principles before being given an assignment to solve this at compile time. If not, that is what you should talk to your instructor about.

  9. #9
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    My mistake. I've never seen template metaprogramming before. What are its advantages?

  10. #10
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    THe hint given is how does f(n+1) relate to f(n)
    Well? How does f(n+1) relate to f(n)?
    That is essential to the solution. They gave you that hint for a reason!

    5f(n) = (3+2)(3^n-2^n) = 3^(n+1) - 2^(n+1) + 2*3(3^(n-1)-2^(n-1))
    <=>
    f(n+1) = 5f(n) - 6f(n-1)

    Wow, a recursive formula! If you know really basic template metaprogramming, you will know how to solve this.
    Last edited by Sang-drax; 04-08-2005 at 05:34 AM. Reason: Missed an 'f'
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by joshdick
    My mistake. I've never seen template metaprogramming before. What are its advantages?
    Template metaprogramming can do a few things.
    First, it can make the code more readable, by letting you write weird values as the result of a formula, such that they make more sense to the reader, without sacrificing runtime performance.
    Second, it can partially precompute algorithms, which can for example mean unrolled loops, thus making the resulting code run faster. While that ought to be the job of the compiler, there are situations where the compiler alone can't do it.

    What it cannot do is do any calculations with values that aren't known at compile time.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM
  2. Convert VisualC++ template to gcc
    By efgee in forum C++ Programming
    Replies: 16
    Last Post: 10-04-2007, 07:25 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM