CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    [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.

  2. #2
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    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.

  3. #3
    Join Date
    Aug 2007
    Posts
    858

    Re: Tenth Power Template Metaprogramming

    Why causing this issue ?
    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.

  4. #4
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    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

  5. #5
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    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.

  6. #6
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    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.

  7. #7
    Join Date
    Aug 2007
    Posts
    858

    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.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: [RESOLVED] Tenth Power Template Metaprogramming

    Quote Originally Posted by Peter_APIIT View Post
    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

  9. #9
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: [RESOLVED] Tenth Power Template Metaprogramming

    Quote Originally Posted by Paul McKenzie View Post
    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.

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449

    Re: [RESOLVED] Tenth Power Template Metaprogramming

    Quote Originally Posted by Peter_APIIT View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured