Problem with Partial Template Specialization and Borland C++ Builder

A

Alex@L

I've a problem with following simple code (not really usefull):
//---------------------------------------------------------------------------
#pragma hdrstop
#include "stdio.h"
#include <iostream>

enum eTest1{et1_1, et1_2} e1;
enum eTest2{et2_1, et2_2} e2;
class ImageBase
{
public:
int rows;
int columns;
};
template <eTest1 EN1, typename eTest2 EN2>
//template <int EN1, typename eTest2 EN2>
class Image : private ImageBase
{
public:
Image()
{
rows=1;
columns=1;
std::cout<<"std CTOR"<<std::endl;
// use 'columns' and 'rows'
}
void out(){std::cout<<columns<<" "<<rows<<std::endl;};
};

template <eTest1 EN1>
//template <int EN1>
class Image<EN1, et2_2> : private ImageBase
{
public:
Image()
{
rows=500;
std::cout<<"columns, 200 CTOR"<<std::endl;
// use 'columns' columns and 200 rows.
}
void out(){std::cout<<columns<<" "<<rows<<std::endl;};
};

int main(int argc, char* argv[])
{
Image<et1_1, et2_1> img1;
img1.out();
Image<et1_1, et2_2> img2;
img2.out();
return 0;
}
//---------------------------------------------------------------------------
I't seem like the partial template specialization work not with my
Borland C++ Builder 5. I got the same behaviour with img1 and img2.
Some CTOR's and some out()-functions are called in both casses.
But if I use templates with ...int... it works fine.

Can somebody, please, explain me, what goes here wrong.

Thx, Alexander
 
V

Victor Bazarov

Alex@L said:
I've a problem with following simple code (not really usefull):
//---------------------------------------------------------------------------

#pragma hdrstop
#include "stdio.h"
#include <iostream>

enum eTest1{et1_1, et1_2} e1;
enum eTest2{et2_1, et2_2} e2;
class ImageBase
{
public:
int rows;
int columns;
};
template <eTest1 EN1, typename eTest2 EN2>
^^^^^^^^^^^^^^^^^^^
Isn't this supposed to be "eTest2 EN2" (without the 'typename')?
//template <int EN1, typename eTest2 EN2>
class Image : private ImageBase
{
public:
Image()
{
rows=1;
columns=1;
std::cout<<"std CTOR"<<std::endl;
// use 'columns' and 'rows'
}
void out(){std::cout<<columns<<" "<<rows<<std::endl;};
};

template <eTest1 EN1>
//template <int EN1>
class Image<EN1, et2_2> : private ImageBase
{
public:
Image()
{
rows=500;
std::cout<<"columns, 200 CTOR"<<std::endl;
// use 'columns' columns and 200 rows.

That's misleading. First of all, it's 500 (FIVE hundred), not 200 (TWO
hundred). Second, 'columns' member is _uninitalised_.
}
void out(){std::cout<<columns<<" "<<rows<<std::endl;};
};

int main(int argc, char* argv[])
{
Image<et1_1, et2_1> img1;
img1.out();
Image<et1_1, et2_2> img2;
img2.out();
return 0;
}
//---------------------------------------------------------------------------

I't seem like the partial template specialization work not with my
Borland C++ Builder 5. I got the same behaviour with img1 and img2.

Have you tried dropping the 'typename' keyword? Maybe it gets confused
by it and can't correctly generate the code...
Some CTOR's and some out()-functions are called in both casses.
But if I use templates with ...int... it works fine.

Can somebody, please, explain me, what goes here wrong.

Hard to say. VC++ calls different constructors and different 'out'.
Could be a bug.

V
 
A

Alex@L

Victor said:
^^^^^^^^^^^^^^^^^^^
Isn't this supposed to be "eTest2 EN2" (without the 'typename')?

I've tried both, with and without 'typename'. That's have no difference.
That's misleading. First of all, it's 500 (FIVE hundred), not 200 (TWO
hundred). Second, 'columns' member is _uninitalised_.

Right. But this simple class was only constructed to show the problem.
I've wrote above "(not really usefull)".
}
void out(){std::cout<<columns<<" "<<rows<<std::endl;};
};

int main(int argc, char* argv[])
{
Image<et1_1, et2_1> img1;
img1.out();
Image<et1_1, et2_2> img2;
img2.out();
return 0;
}
//---------------------------------------------------------------------------

I't seem like the partial template specialization work not with my
Borland C++ Builder 5. I got the same behaviour with img1 and img2.


Have you tried dropping the 'typename' keyword? Maybe it gets confused
by it and can't correctly generate the code...

Yes. I've also tryed 'typedef'ed enums, 'class' (for abstract EN1 and
Hard to say. VC++ calls different constructors and different 'out'.
Could be a bug.
Thanks. I think also, that may be a bug. But another peoples assures,
this must work, but they give no working examples.
 
M

Mike Wahler

Alex@L said:
//--------------------------------------------------------------------------
-
^^^^^^^^^^^^^^^^^^^
Isn't this supposed to be "eTest2 EN2" (without the 'typename')?

I've tried both, with and without 'typename'. That's have no difference.
That's misleading. First of all, it's 500 (FIVE hundred), not 200 (TWO
hundred). Second, 'columns' member is _uninitalised_.

Right. But this simple class was only constructed to show the problem.
I've wrote above "(not really usefull)".
}
void out(){std::cout<<columns<<" "<<rows<<std::endl;};
};

int main(int argc, char* argv[])
{
Image<et1_1, et2_1> img1;
img1.out();
Image<et1_1, et2_2> img2;
img2.out();
return 0;
}
//--------------------------------------------------------------------------
-
Have you tried dropping the 'typename' keyword? Maybe it gets confused
by it and can't correctly generate the code...

Yes. I've also tryed 'typedef'ed enums, 'class' (for abstract EN1 and
Hard to say. VC++ calls different constructors and different 'out'.
Could be a bug.
Thanks. I think also, that may be a bug. But another peoples assures,
this must work, but they give no working examples.

If you're convinced you have some code that is correct, yet
your compiler rejects it or doesn't create code that behaves
correctly, try another compiler (or two, or three). Many compilers
do have bugs.

One thing to try that has a good reputation for 'correctness'
is the Comeau 'try it' online compiler at www.comeaucomputing.com
(However, I hear they've been having some technical difficulties
with their web site, so this might not be possible at the moment).

-Mike
 
G

Greg Comeau

If you're convinced you have some code that is correct, yet
your compiler rejects it or doesn't create code that behaves
correctly, try another compiler (or two, or three). Many compilers
do have bugs.

Good advice. Furthermore, try other compilers _even when_ your
compiler behaves correctly.
One thing to try that has a good reputation for 'correctness'
is the Comeau 'try it' online compiler at www.comeaucomputing.com
(However, I hear they've been having some technical difficulties
with their web site, so this might not be possible at the moment).

More good advice :) For some people we never appeared down,
for those who found it down, it's been back up for sure since
Monday evening at the absolute latest (meaning for most people
it was back up before that). In short, internet seccurity was
breached (not for comeaucomputing.com, we were actually not
touched and up the whole time, but in the greater sense),
and hopefully the breach results in policy changes at ICANN.
We were not the only ones effected. For more details see
http://tinyurl.com/66rxz rather than discuss it here.
 
A

Alex@L

Mike said:
Alex@L said:
Victor said:
Alex@L wrote:


I've a problem with following simple code (not really usefull):
//--------------------------------------------------------------------------
-
#pragma hdrstop
#include "stdio.h"
#include <iostream>

enum eTest1{et1_1, et1_2} e1;
enum eTest2{et2_1, et2_2} e2;
class ImageBase
{
public:
int rows;
int columns;
};
template <eTest1 EN1, typename eTest2 EN2>

^^^^^^^^^^^^^^^^^^^
Isn't this supposed to be "eTest2 EN2" (without the 'typename')?

I've tried both, with and without 'typename'. That's have no difference.

//template <int EN1, typename eTest2 EN2>
class Image : private ImageBase
{
public:
Image()
{
rows=1;
columns=1;
std::cout<<"std CTOR"<<std::endl;
// use 'columns' and 'rows'
}
void out(){std::cout<<columns<<" "<<rows<<std::endl;};
};

template <eTest1 EN1>
//template <int EN1>
class Image<EN1, et2_2> : private ImageBase
{
public:
Image()
{
rows=500;
std::cout<<"columns, 200 CTOR"<<std::endl;
// use 'columns' columns and 200 rows.


That's misleading. First of all, it's 500 (FIVE hundred), not 200 (TWO
hundred). Second, 'columns' member is _uninitalised_.

Right. But this simple class was only constructed to show the problem.
I've wrote above "(not really usefull)".

}
void out(){std::cout<<columns<<" "<<rows<<std::endl;};
};

int main(int argc, char* argv[])
{
Image<et1_1, et2_1> img1;
img1.out();
Image<et1_1, et2_2> img2;
img2.out();
return 0;
}
//--------------------------------------------------------------------------
-
I't seem like the partial template specialization work not with my
Borland C++ Builder 5. I got the same behaviour with img1 and img2.


Have you tried dropping the 'typename' keyword? Maybe it gets confused
by it and can't correctly generate the code...

Yes. I've also tryed 'typedef'ed enums, 'class' (for abstract EN1 and
EN) and without all (i.e template<EN1, EN2>class...). In all cases I got
the same behaviour.

Some CTOR's and some out()-functions are called in both casses.
But if I use templates with ...int... it works fine.

Can somebody, please, explain me, what goes here wrong.


Hard to say. VC++ calls different constructors and different 'out'.
Could be a bug.

Thanks. I think also, that may be a bug. But another peoples assures,
this must work, but they give no working examples.


If you're convinced you have some code that is correct, yet
your compiler rejects it or doesn't create code that behaves
correctly, try another compiler (or two, or three). Many compilers
do have bugs.

One thing to try that has a good reputation for 'correctness'
is the Comeau 'try it' online compiler at www.comeaucomputing.com
(However, I hear they've been having some technical difficulties
with their web site, so this might not be possible at the moment).

-Mike
Many thanks, but the problem is, I MUST use this compiler at the
moment... Later it maybe newest C#, but also with binding to one comiler.

Alex
 
M

Mike Wahler

Alex@L said:
Many thanks, but the problem is, I MUST use this compiler at the
moment...

I understand, and have been in a similar situation many times.
This is an example of what I call the "Welcome To The Real World"
syndrome. (I encounter it more often in the context of time and
budget issues, but I digress...)

If you have some correct code which your compiler cannot handle,
and are not allowed to use a different compiler, then you'll need
to create code to do the same thing using other language features
which your compiler does handle correctly. IMO being able to deal
with this kind of thing is what separates the 'men from the boys'
in "programmer-land." Remember that as a programmer, your primary
role is not writing code, but *solving problems*. Ideally, you're
creating something that uses a computer to solve someone else's
problem (e.g. keeping their books). But sometimes (or often),
during your act of creating, you encounter your *own* problems.
Use your skills. Solve the problem. Move on to the next.
[End lecture]. :)

HTH,
-Mike
 

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

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top