Half templates does not work.

N

Nephi Immortal

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;
}
 
R

red floyd

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).
 
N

Nephi Immortal

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?
 
N

Noah Roberts

        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/
 
N

Nephi Immortal

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

Ian Collins

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".
 
M

Miles Bader

From: Nephi Immortal said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top