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