Half templates does not work.

Discussion in 'C++' started by Nephi Immortal, Sep 30, 2011.

  1. I created two templates. First template is like trait types. Second
    template includes first template. Everything works fine except second
    template.

    The error message reports:

    Main.cpp
    c:\my projects\main.cpp(230): fatal error C1001: An internal error has
    occurred in the compiler.
    (compiler file 'msc1.cpp', line 1420)
    To work around this problem, try simplifying or changing the
    program near the locations listed above.
    Please choose the Technical Support command on the Visual C++
    Help menu, or open the Technical Support help file for more
    information

    I searched several websites. I was unable to find the solution. I
    checked C++ Template - The Complete Guide book and the section shows
    4.3 Restrictions for Nontype Template Parameters.

    Do you have the solution? Is there another way to change the code in
    order to work?

    enum ETypes
    {
    e_Type1,
    e_Type2
    };

    template< ETypes Type >
    struct Types;

    template<>
    struct Types< e_Type1 >
    {
    typedef char value_type;
    value_type x;
    };

    template<>
    struct Types< e_Type2 >
    {
    typedef short value_type;
    value_type x;
    };

    template< ETypes Type, typename Traits = Types< Type > >
    struct t_Type
    {
    typename typedef Traits::value_type value_type;

    value_type data;
    };

    template< ETypes L, ETypes R >
    short operator+( const Types< L > &l, const Types< R > &r )
    {
    typedef Types< L > t_L;
    typename typedef t_L::value_type value_type;

    value_type t = static_cast< value_type >( l.x + r.x );
    return t;
    }

    template< ETypes L, typename R >
    short operator+( const Types< L > &l, const R &r )
    {
    typedef Types< L > t_L;
    typename typedef t_L::value_type value_type;

    value_type t = static_cast< value_type >( l.x + r );
    return t;
    }

    template< typename L, ETypes R >
    short operator+( const L &l, const Types< R > &r )
    {
    typedef Types< R > t_R;
    typename typedef t_R::value_type value_type;

    value_type t = static_cast< value_type >( l + r.x );
    return t;
    }

    template< ETypes L, ETypes R >
    short operator+( const t_Type< L > &l, const t_Type< R > &r )
    {
    typedef t_Type< L > t_L;
    typename typedef t_L::value_type value_type;

    value_type t = static_cast< value_type >( l.data + r.data );
    return t;
    }

    template< ETypes L, typename R >
    short operator+( const t_Type< L > &l, const R &r )
    {
    typedef t_Type< L > t_L;
    typename typedef t_L::value_type value_type;

    value_type t = static_cast< value_type >( l.data + r );
    return t;
    }

    template< typename L, ETypes R >
    short operator+( const L &l, const t_Type< R > &r )
    {
    typedef t_Type< R > t_R;
    typename typedef t_R::value_type value_type;

    value_type t = static_cast< value_type >( l + r.data );
    return t;
    }


    int main()
    {
    Types< e_Type1 > a1; a1.x = 0x10;
    Types< e_Type1 > a2; a2.x = 0x20;

    Types< e_Type2 > b1; b1.x = 0x30;
    Types< e_Type2 > b2; b2.x = 0x40;

    short t;

    t = a1 + a2; // OK
    t = a1 + 10; // OK
    t = 20 + a2; // OK

    t = a1 + b1; // OK Two different combined types are allowed through
    type conversion.

    t_Type< e_Type1 > c1; c1.data = 0x10;
    t_Type< e_Type1 > c2; c2.data = 0x20;

    t_Type< e_Type2 > d1; d1.data = 0x30;
    t_Type< e_Type2 > d2; d2.data = 0x40;

    t = c1 + c2; // OK
    t = c1 + 10; // OK
    t = 20 + c2; // C++ Compiler Bug

    t = c1 + d1; // C++ Compiler Bug

    return 0;
    }
     
    Nephi Immortal, Sep 30, 2011
    #1
    1. Advertising

  2. Nephi Immortal

    red floyd Guest

    On 9/29/2011 4:25 PM, Nephi Immortal wrote:
    > I created two templates. First template is like trait types. Second
    > template includes first template. Everything works fine except second
    > template.
    >
    > The error message reports:
    >
    > Main.cpp
    > c:\my projects\main.cpp(230): fatal error C1001: An internal error has
    > occurred in the compiler.
    > (compiler file 'msc1.cpp', line 1420)
    > To work around this problem, try simplifying or changing the
    > program near the locations listed above.
    > Please choose the Technical Support command on the Visual C++
    > Help menu, or open the Technical Support help file for more
    > information
    >
    > I searched several websites. I was unable to find the solution. I
    > checked C++ Template - The Complete Guide book and the section shows
    > 4.3 Restrictions for Nontype Template Parameters.
    >

    There may be no problem with your code. It is reporting an internal
    error in the compiler (it got in a state it shouldn't have). Check
    with a different compiler (or even a different version of the MSVC).
     
    red floyd, Sep 30, 2011
    #2
    1. Advertising

  3. On Sep 29, 11:16 pm, red floyd <> wrote:
    > On 9/29/2011 4:25 PM, Nephi Immortal wrote:
    >
    >
    >
    > >    I created two templates.  First template is like trait types.  Second
    > > template includes first template.  Everything works fine except second
    > > template.

    >
    > >    The error message reports:

    >
    > >    Main.cpp
    > > c:\my projects\main.cpp(230): fatal error C1001: An internal error has
    > > occurred in the compiler.
    > >    (compiler file 'msc1.cpp', line 1420)
    > >     To work around this problem, try simplifying or changing the
    > > program near the locations listed above.
    > >    Please choose the Technical Support command on the Visual C++
    > >     Help menu, or open the Technical Support help file for more
    > > information

    >
    > >    I searched several websites.  I was unable to find the solution.  I
    > > checked C++ Template - The Complete Guide book and the section shows
    > > 4.3 Restrictions for Nontype Template Parameters.

    >
    > There may be no problem with your code.  It is reporting an internal
    > error in the compiler (it got in a state it shouldn't have).  Check
    > with a different compiler (or even a different version of the MSVC).- Hide quoted text -
    >
    > - Show quoted text -


    If you read several websites, it tells the same problem on MSVC 2005,
    2008, and 2010.
    There is no fix. Do you think Intel C++ Compiler will work? What
    about GCC? Have
    you tested it before?
     
    Nephi Immortal, Sep 30, 2011
    #3
  4. Nephi Immortal

    Noah Roberts Guest

    On Sep 29, 4:25 pm, Nephi Immortal <> wrote:
    >         I created two templates.  First template is like trait types.  Second
    > template includes first template.  Everything works fine except second
    > template.
    >
    >         The error message reports:
    >
    >   Main.cpp
    > c:\my projects\main.cpp(230): fatal error C1001: An internal error has
    > occurred in the compiler.
    >   (compiler file 'msc1.cpp', line 1420)
    >    To work around this problem, try simplifying or changing the
    > program near the locations listed above.
    >   Please choose the Technical Support command on the Visual C++
    >    Help menu, or open the Technical Support help file for more
    > information


    Yep. MSVC is broken when it comes to templates and a lot of other
    things.

    Try moving around some expressions. Include headers in different
    order. Functions in different order, etc... Many times when you
    force the compiler to process the code in a different order it
    works.

    For example, I wrote this reflection thing that would work just fine
    until it was used in a cpp that also included signals2.hpp...even if
    it wasn't itself using it. Making sure the reflection headers were
    included first fixed the problem.

    Make sure your code is very well tested though. I've seen MSVC
    generate garbage object code when you try to coax it into compiling
    something it's crashing on. I was once trying to assign a bunch of
    lambdas into a function table...when I finally talked it into
    compiling, it generated function objects with bad pointers inside.

    For all its massive improvements, MSVC++ is still pretty aweful in the
    template area truly capable of only the simplest of constructs, and
    many of those it gets wrong as well:
    http://crazycpp.wordpress.com/2011/04/20/msvc-bug-thats-constantly-getting-me/
     
    Noah Roberts, Sep 30, 2011
    #4
  5. On Sep 30, 11:25 am, Noah Roberts <> wrote:
    > On Sep 29, 4:25 pm, Nephi Immortal <> wrote:
    >
    > >         I created two templates.  First template is like trait types.  Second
    > > template includes first template.  Everything works fine except second
    > > template.

    >
    > >         The error message reports:

    >
    > >   Main.cpp
    > > c:\my projects\main.cpp(230): fatal error C1001: An internal error has
    > > occurred in the compiler.
    > >   (compiler file 'msc1.cpp', line 1420)
    > >    To work around this problem, try simplifying or changing the
    > > program near the locations listed above.
    > >   Please choose the Technical Support command on the Visual C++
    > >    Help menu, or open the Technical Support help file for more
    > > information

    >
    > Yep.  MSVC is broken when it comes to templates and a lot of other
    > things.
    >
    > Try moving around some expressions.  Include headers in different
    > order.  Functions in different order, etc...  Many times when you
    > force the compiler to process the code in a different order it
    > works.
    >
    > For example, I wrote this reflection thing that would work just fine
    > until it was used in a cpp that also included signals2.hpp...even if
    > it wasn't itself using it.  Making sure the reflection headers were
    > included first fixed the problem.
    >
    > Make sure your code is very well tested though.  I've seen MSVC
    > generate garbage object code when you try to coax it into compiling
    > something it's crashing on.  I was once trying to assign a bunch of
    > lambdas into a function table...when I finally talked it into
    > compiling, it generated function objects with bad pointers inside.
    >
    > For all its massive improvements, MSVC++ is still pretty aweful in the
    > template area truly capable of only the simplest of constructs, and
    > many of those it gets wrong as well:http://crazycpp.wordpress.com/2011/04/20/msvc-bug-thats-constantly-ge...


    Of course, I agree with you. I always write generic interface on the
    header and generic implementation on the source code. I am aware that
    explicit specialization does not work that way, but the header always
    includes source code before main.cpp includes header. The C++
    Compiler works fine as normal before.
    My question is – have you tried to use Intel C++ Compiler and GCC C++
    Compiler to see if they work already? Have you tested it?
    I do not necessarily go ahead to take your recommendation, but my
    code is very simple. I am going to try and remove default parameter
    like this below.

    From:
    template< ETypes Type, typename Traits = Types< Type > >
    struct t_Type {};

    To:
    template< ETypes Type >
    struct t_Type
    {
    typedef Types< Type > Trait_Type;
    };

    Put typedef inside class body to include Types< Type >. MSVC++
    Compiler should compile without any error. I have not tried this yet,
    but I am going to test it today.
     
    Nephi Immortal, Sep 30, 2011
    #5
  6. Nephi Immortal

    Ian Collins Guest

    On 09/30/11 12:25 PM, Nephi Immortal wrote:
    > I created two templates. First template is like trait types. Second
    > template includes first template. Everything works fine except second
    > template.
    >
    > The error message reports:
    >
    > Main.cpp
    > c:\my projects\main.cpp(230): fatal error C1001: An internal error has
    > occurred in the compiler.
    > (compiler file 'msc1.cpp', line 1420)
    > To work around this problem, try simplifying or changing the
    > program near the locations listed above.
    > Please choose the Technical Support command on the Visual C++
    > Help menu, or open the Technical Support help file for more
    > information
    >
    > I searched several websites. I was unable to find the solution. I
    > checked C++ Template - The Complete Guide book and the section shows
    > 4.3 Restrictions for Nontype Template Parameters.
    >
    > Do you have the solution? Is there another way to change the code in
    > order to work?
    >
    > enum ETypes
    > {
    > e_Type1,
    > e_Type2
    > };
    >
    > template< ETypes Type>
    > struct Types;
    >
    > template<>
    > struct Types< e_Type1>
    > {
    > typedef char value_type;
    > value_type x;
    > };
    >
    > template<>
    > struct Types< e_Type2>
    > {
    > typedef short value_type;
    > value_type x;
    > };
    >
    > template< ETypes Type, typename Traits = Types< Type> >
    > struct t_Type
    > {
    > typename typedef Traits::value_type value_type;


    Try changing all occurrences of "typename typedef" to "typedef typename".

    --
    Ian Collins
     
    Ian Collins, Sep 30, 2011
    #6
  7. Nephi Immortal

    Miles Bader Guest

    From: Nephi Immortal <>
    > On Sep 29, 11:16 pm, red floyd <> wrote:
    > > There may be no problem with your code.  It is reporting an
    > > internal error in the compiler (it got in a state it shouldn't
    > > have).  Check with a different compiler (or even a different
    > > version of the MSVC).

    >
    > If you read several websites, it tells the same problem on MSVC
    > 2005, 2008, and 2010. There is no fix. Do you think Intel C++
    > Compiler will work? What about GCC? Have you tested it before?


    The code contains a persistent error: it uses "typename typedef ..."
    in a bunch of places, which should really be "typedef typename ...".

    After fixing that problem, it compiles fine in both g++ and clang++.

    -Miles

    --
    Americans are broad-minded people. They'll accept the fact that a person can
    be an alcoholic, a dope fiend, a wife beater, and even a newspaperman, but if
    a man doesn't drive, there is something wrong with him. -- Art Buchwald
     
    Miles Bader, Sep 30, 2011
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JKop
    Replies:
    3
    Views:
    521
  2. recover
    Replies:
    2
    Views:
    857
    recover
    Jul 25, 2006
  3. trint
    Replies:
    4
    Views:
    724
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Sep 11, 2007
  4. joe chesak
    Replies:
    7
    Views:
    310
    (r.*n){2}
    Sep 23, 2010
  5. Mike Ballard
    Replies:
    6
    Views:
    285
    Dr.Ruud
    Nov 15, 2005
Loading...

Share This Page