Calculating value during compile-time

Kimpan
Kimpan used Ask the Experts™
on
Using template, write a function template Fibonacci so that the result is generated during compile-time and not run-time. So you can write like this in your code:

const int i = Fibonacci<10>::value;


I was an eyeopener for me. Will it be for you?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
oh, and write a power template as well so you can write

const int i = Pow<5,3>::value;

(i = 5 * 5 * 5)

Commented:
Kimpan,
Do you need this answer for yourself, or are you just posting a puzzle type question?
EOL

Commented:
Game programming gems volume one.
But yes, template metaprogramming is a topic for real language geeks. Btw. check out http://www.flipcode.com/tutorials/tut_fastmath.shtml .

Cheers EOL
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

EOL

Commented:
Also be sure to not to miss http://www.boost.org/libs/libraries.htm#Generic .
jkr
Top Expert 2012

Commented:
I think Kimpan is thinking of (partial) template specialization and a puzzle like

template<int M, int N>
class Power {
public:
enum { value = M * Power<M-1,N>::value};
};

class Power<0, int N> {

public:
enum { value = 1};
};

template <int N>
class Fibonacci {
public:
enum { value = Fibonacci<N-2>::num + Fibonacci<N-1>::value};
};

class Fibonacci<2> {
public:
enum { value = 1};
};

class Fibonacci<1> {
public:
enum { value = 1};
};

Cannot test the 'Power' template though, as VC++ does not support partial specialization :-(
EOL

Commented:
here's the gems example

template <unsigned int n> struct fib
{
     enum
     {
          val = fib< n-1 >::val + fib< n-2 >::val
     };
};

template <> struct fib< 0 > { enum{ val = 0 }; };
template <> struct fib< 1 > { enum{ val = 1 }; };
Top Expert 2012
Commented:
Ooops - 'Power' should of course be

template<int M, int N>
class Power {
public:
enum { value = M * Power<M,N-1>::value};
};

class Power<int M, 0> {

public:
enum { value = 1};
};
EOL

Commented:
isn't

template<int M, int N>
class Power {
public:
enum { value = M * Power<M-1,N>::value};
};

class Power<0, int N> {

public:
enum { value = 1};
};

wrong and

template<int M, int N>
class Power {
public:
enum { value = M * Power<M,N-1>::value};
};

class Power<0, int N> {

public:
enum { value = 1};
};

right?
EOL

Commented:
oh, forgott the specialisation

class Power<M, 1> {

public:
enum { value = M};
};
jkr
Top Expert 2012

Commented:
Well, noticed it the same minute :o)
EOL

Commented:
har har, c++ leads to multi threaded thinking that syncs brains.
EOL

Commented:
unfortunately you can't make so nice "power" code in VC7. Because VC7 still doesn't support partial specialisation template arguments.

Template support under VC7 is real bad, most of boost you can't use on this ***** compiler. BUT, it's better then in VC6 and I heard rumors about a next version that should be compatible to all your boost that belongs to gnu.

Author

Commented:
You guys shouldn't be allowed to answer my questions..
EOL

Commented:
rofl :o)

Author

Commented:
I thought some others who didn't know about 'template metaprogramming' would find it fascinating. Because I was when I learned this technique during a course two weeks ago. And yes, the teacher showed us how it could be used to optimze matrix operations by eliminating temporary object construction with it.

I wanted to inform others who didn't know about this particular 'feature' and I also felt like spending some points.

So I would like jkr and EOL to share equal amount of the points advertised. How do I achieve that? (But if you ask me, I don't think you should get any. You ruined the fun for many who might have tried :) )

EOL

Commented:
hm, if you want to give us equal amounts you have the one choice to make a "points for EOL" or "points for jkr" question.
Better idea would be to talk to the admin and ask him nicely if he could implement point spreading or something. Ah, and while you do that, please ask him to add a parsing step in his forum script so we can have our source code postings nicely in courier preformatted.

Author

Commented:
Geeze, are you asking me to pick up your fight? :)
I will quote you about the formatting part to the admin.

Points reduced for a split.  You can now accept one experts comment as an answer.  After that, make another question in this topic area for the other expert.  Make the question for the amount of points intended.  Label the question, "Question for (Expert Name) and post this question number in the base of the question, i.e. For your assistance in question # --------


Computer101
E-E Admin
jkr
Top Expert 2012

Commented:
>>the teacher showed us how it could be used to optimze
>>matrix operations by eliminating temporary object
>>construction with it.

May I ask what you are studying? I originally got an enginnering degree in tech. physics, and in the early '90s, that kind of optimization wold really have helped me (a SPARC-II ran almost a week when caclculationg my most complex model - in MATLAB, though), but C++ was far from being a standard...
EOL

Commented:
And template metaprogramming was very far from beeing possible anytime soon.

As goes for me I am studying nothing, just picked programming up on the way with a bit learning at home. Ok, maybe working in a maintenance C++ project the last 1 1/2 years has helped a bit. I started programming two years ago.

Author

Commented:
I am almost done studying, I have my exam graduate work left. Although I don't know when I will have time to finish it since I began to work before it was completed. Stupid me. (I wanted the money!) I am thinking of taking a break and get my master degree in computer science next year (mostly to make my mom happy)

But this 'metaprogramming' came to my knowledge thru the course at work with topics C++ for embedded systems.

 

Author

Commented:
jkr, as eng. in tech. physics, you should work with solving mysteries of the Universe and not live in disgrace as a computer nerd. You really hit the bottom. :)
jkr
Top Expert 2012

Commented:
Resorted to IT after they turned down my proposal for a new warp drive :o)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial