Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #10597 > unrolled thread

Half templates does not work.

Started byNephi Immortal <immortalnephi@gmail.com>
First post2011-09-29 16:25 -0700
Last post2011-10-01 09:08 +1300
Articles 7 — 5 participants

Back to article view | Back to comp.lang.c++


Contents

  Half templates does not work. Nephi Immortal <immortalnephi@gmail.com> - 2011-09-29 16:25 -0700
    Re: Half templates does not work. red floyd <no.spam.here@its.invalid> - 2011-09-29 21:16 -0700
      Re: Half templates does not work. Nephi Immortal <immortalnephi@gmail.com> - 2011-09-30 05:15 -0700
        Re: Half templates does not work. Miles Bader <miles@gnu.org> - 2011-10-01 07:35 +0900
    Re: Half templates does not work. Noah Roberts <roberts.noah@gmail.com> - 2011-09-30 09:25 -0700
      Re: Half templates does not work. Nephi Immortal <immortalnephi@gmail.com> - 2011-09-30 12:17 -0700
    Re: Half templates does not work. Ian Collins <ian-news@hotmail.com> - 2011-10-01 09:08 +1300

#10597 — Half templates does not work.

FromNephi Immortal <immortalnephi@gmail.com>
Date2011-09-29 16:25 -0700
SubjectHalf templates does not work.
Message-ID<d82bd711-cf85-4a8b-acda-cbd4482f2f4d@18g2000yqz.googlegroups.com>
	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;
}

[toc] | [next] | [standalone]


#10604

Fromred floyd <no.spam.here@its.invalid>
Date2011-09-29 21:16 -0700
Message-ID<j63fra$npc$1@dont-email.me>
In reply to#10597
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).

[toc] | [prev] | [next] | [standalone]


#10609

FromNephi Immortal <immortalnephi@gmail.com>
Date2011-09-30 05:15 -0700
Message-ID<50e167fe-13e3-47fb-9e97-2c363206b688@d26g2000vby.googlegroups.com>
In reply to#10604
On Sep 29, 11:16 pm, red floyd <no.spam.h...@its.invalid> 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?

[toc] | [prev] | [next] | [standalone]


#10616

FromMiles Bader <miles@gnu.org>
Date2011-10-01 07:35 +0900
Message-ID<877h4p4q0p.fsf@catnip.gol.com>
In reply to#10609
From: Nephi Immortal <immortalnephi@gmail.com>
> On Sep 29, 11:16 pm, red floyd <no.spam.h...@its.invalid> 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

[toc] | [prev] | [next] | [standalone]


#10610

FromNoah Roberts <roberts.noah@gmail.com>
Date2011-09-30 09:25 -0700
Message-ID<8583dacf-64aa-4a66-b324-7d4e62aa81d8@e9g2000vby.googlegroups.com>
In reply to#10597
On Sep 29, 4:25 pm, Nephi Immortal <immortalne...@gmail.com> 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/

[toc] | [prev] | [next] | [standalone]


#10611

FromNephi Immortal <immortalnephi@gmail.com>
Date2011-09-30 12:17 -0700
Message-ID<e0039ebe-959a-4d31-9c3b-54c0adf09b00@dk6g2000vbb.googlegroups.com>
In reply to#10610
On Sep 30, 11:25 am, Noah Roberts <roberts.n...@gmail.com> wrote:
> On Sep 29, 4:25 pm, Nephi Immortal <immortalne...@gmail.com> 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.

[toc] | [prev] | [next] | [standalone]


#10614

FromIan Collins <ian-news@hotmail.com>
Date2011-10-01 09:08 +1300
Message-ID<9emltoFudmU1@mid.individual.net>
In reply to#10597
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web