Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Half templates does not work.

Reply
Thread Tools

Half templates does not work.

 
 
Nephi Immortal
Guest
Posts: n/a
 
      09-29-2011
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;
}


 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      09-30-2011
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).


 
Reply With Quote
 
 
 
 
Nephi Immortal
Guest
Posts: n/a
 
      09-30-2011
On Sep 29, 11:16*pm, red floyd <(E-Mail Removed)> 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?
 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      09-30-2011
On Sep 29, 4:25*pm, Nephi Immortal <(E-Mail Removed)> 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/0...ly-getting-me/
 
Reply With Quote
 
Nephi Immortal
Guest
Posts: n/a
 
      09-30-2011
On Sep 30, 11:25*am, Noah Roberts <(E-Mail Removed)> wrote:
> On Sep 29, 4:25*pm, Nephi Immortal <(E-Mail Removed)> 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/0...-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.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      09-30-2011
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
 
Reply With Quote
 
Miles Bader
Guest
Posts: n/a
 
      09-30-2011
From: Nephi Immortal <(E-Mail Removed)>
> On Sep 29, 11:16*pm, red floyd <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
comparing the first half of a string to the second half joe chesak Ruby 7 09-23-2010 03:34 AM
ie7 displaying only half of page (cuts page in half)... trint ASP .Net 4 09-11-2007 10:56 AM
regexp that matches half then conditionally excludes the other half Mike Ballard Perl Misc 6 11-15-2005 03:26 PM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM



Advertisments