-
June 12th, 2009, 08:13 AM
#1
[RESOLVED] Tenth Power Template Metaprogramming
Hello to all, recently i have develop a very basic template metaprogramming in C++ about power function.
I have encounter error which is fatal error C1001: An internal error has occurred in the compiler.
Below is my program:
Code:
#ifndef MYPOWER_H
#define MYPOWER_H
#ifdef WIN32
// Window Specific Function
typedef unsigned int size_t;
// Value of baseLenght must be known at CT
template <size_t baseLength>
class MyPower
{
public:
enum {result = 10 * MyPower<baseLength - 1>::result };
};
// Template Specialization when size_t value = 0
template <>
class MyPower<0>
{
public:
enum {result = 1};
};
#else
// Posix Specific Function
#endif
#endif
#include <iostream>
#include "MyPower.h"
using namespace std;
// ========================================
// ========================================
// ========================================
int main()
{
cout << MyPower<2006546545>::result;
return 0;
}
// ========================================
Why causing this issue ?
How to change size of result in order to avoid integral constant overflow ?
Thanks.
Thanks for your help.
-
June 12th, 2009, 08:48 AM
#2
Re: Tenth Power Template Metaprogramming
So, you are expecting the compiler to instantiate code for 2006546545 different classes and succeed?
Even if it did, the following generated code is hardly likely to compile:
Code:
template <2006546545>
class MyPower
{
public:
enum {result = 100000000000000000000000000000000000000000000000000000000000000000000000000000..... /* a couple more zeroes */ };
};
Last edited by treuss; June 12th, 2009 at 08:52 AM.
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
June 12th, 2009, 10:14 AM
#3
Re: Tenth Power Template Metaprogramming
You're asking the compiler to do something far beyond what it's capable of (instantiating more than 2 billion templates).
How to change size of result in order to avoid integral constant overflow ?
You can't. The largest value of your template that will fit in a 32 bit integer is MyPower<9>.... and you're going 2 billion orders of magnitude beyond that. I don't even know if most big number libraries can handle values that large, at least on an average home PC.
-
June 12th, 2009, 02:17 PM
#4
Re: Tenth Power Template Metaprogramming
Instantiating MyPower<n> will result in the compiler trying to compute 10^n (and instatiating n classes in the process). Judging by the title of your thread "Tenth Power Template Metaprogramming" and the fact that you've tried to instantiate MyPower<2006546545>, I'm guessing that wasn't the intended purpose of MyPower<>?
Old Unix programmers never die, they just mv to /dev/null
-
June 13th, 2009, 09:51 AM
#5
Re: Tenth Power Template Metaprogramming
I'm guessing that wasn't the intended purpose of MyPower<>?
I just test the code with large value.
Thanks.
Problem solved.
Thanks for your help.
-
June 13th, 2009, 10:23 PM
#6
Re: [RESOLVED] Tenth Power Template Metaprogramming
How do you all know that 9 is the limit of the template instantiation ?
How to calculate ?
Thanks.
Thanks for your help.
-
June 13th, 2009, 10:34 PM
#7
Re: [RESOLVED] Tenth Power Template Metaprogramming
How do you all know that 9 is the limit of the template instantiation ?
It isn't the limit of template instantiation, it just produces the largest result that can fit in a 32 bit signed integer without overflowing. I've no idea if there are any set limits on how many templates can be instantiated.
-
June 13th, 2009, 11:56 PM
#8
Re: [RESOLVED] Tenth Power Template Metaprogramming
Originally Posted by Peter_APIIT
How do you all know that 9 is the limit of the template instantiation ?
In your program, change the "10" to a "2".
Code:
enum {result = 2 * MyPower<baseLength - 1>::result };
What is the limit of the number of templates instantiated? Given the answer to that, do you now see the relation between that and the template that uses "10"?
Regards,
Paul McKenzie
-
June 14th, 2009, 03:46 AM
#9
Re: [RESOLVED] Tenth Power Template Metaprogramming
Originally Posted by Paul McKenzie
In your program, change the "10" to a "2".
Code:
enum {result = 2 * MyPower<baseLength - 1>::result };
What is the limit of the number of templates instantiated? Given the answer to that, do you now see the relation between that and the template that uses "10"?
Regards,
Paul McKenzie
The limit is 30. When it over 30, compiler display the error again.
I see the relation where
The 30 template argument has the following
2 * 29 = 2147 4836 48
2* 28
The 9 template argument has the following
10 * 8 = 10000 00000
10 * 7
10 * 6
If Tenth power accept 10 as template argument, it will overflow 32 bit signed integer become 1 00000 00000 where maximu value of 32 bit signed integer is 2147 4836 48.
Thanks Paul and other person reply too.
I like Paul approach where ask the beginner to think rather than giving direct answer.
Problem solved.
Thanks for your help.
-
June 14th, 2009, 08:47 AM
#10
Re: [RESOLVED] Tenth Power Template Metaprogramming
Originally Posted by Peter_APIIT
The limit is 30. When it over 30, compiler display the error again.
That's right, you have the answer.
I'm curious though --
If you have a compiler that supports 64-bit ints (native 64-bit ints, not __int64), I wonder what the limit would be (I don't know if the compiler will just reach some other compiler limit, or the limit of a 64-bit int would be reached).
Maybe someone can test it, since I don't have a 64-bit compiler handy right now.
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|