problem with template specialisation

X

Xavier Serrand

The following code don't raise even a warning with DevC++ but with VC6 :
Compiler Error C2908 : explicit specialization; 'template' has already been
specialized from the primary template

with the following code
template <typename T> struct Elem_
{
long x; /* coordonnée x de l'élément */
long y; /* coordonnée y de l'élément */
T el; /* élément d'indice <x,y> */
/* Résolution des collisions */
struct Elem_ * next_x; /* résol. pour des x de même indice selon h1 */
struct Elem_ * next_y; /* résol. pour des y de même indice selon h2 */
};

template <typename T> struct TableDisp_
{
struct Elem_<T> ** table; //Compiler Error C2908 with VC6 only (not with
DevC++ )
long nbEntrees;
};


template <typename T> class MatrixCRS
{
typedef struct Elem_<T> Element; //Compiler Error C2908 with VC6 only (not
with DevC++ )
public:
MatrixCRS() {};
MatrixCRS(T defaultValue);
~MatrixCRS();
T getDefaultElt();
private:
T m_default;
};

I can fix the problem putting all declarations in the class ... but i would
like to find a way to have structs declarations outside the class
MatrixCRS... is it possible ? is it a good idea?

thanks to any advise
 
A

Alf P. Steinbach

* Xavier Serrand:
The following code don't raise even a warning with DevC++ but with VC6 :
Compiler Error C2908 : explicit specialization; 'template' has already been
specialized from the primary template

with the following code
template <typename T> struct Elem_
{
long x; /* coordonnée x de l'élément */
long y; /* coordonnée y de l'élément */
T el; /* élément d'indice <x,y> */
/* Résolution des collisions */
struct Elem_ * next_x; /* résol. pour des x de même indice selon h1 */
struct Elem_ * next_y; /* résol. pour des y de même indice selon h2 */
};

template <typename T> struct TableDisp_
{
struct Elem_<T> ** table; //Compiler Error C2908 with VC6 only (not with
DevC++ )
long nbEntrees;
};


template <typename T> class MatrixCRS
{
typedef struct Elem_<T> Element; //Compiler Error C2908 with VC6 only (not
with DevC++ )
public:
MatrixCRS() {};
MatrixCRS(T defaultValue);
~MatrixCRS();
T getDefaultElt();
private:
T m_default;
};

At a glance I see nothing wrong with the template stuff.

And VC6 should just be ditched, if you have that option.

However, general comments:

* it's not a good idea to reinvent std::vector (your class TableDisp_);
just use std::vector instead.

* Class TableDisp_ lacks a copy constructor, and it needs one; just use
std::vector instead.

* On naming issues, "get" adds no value and detracts from readability in
C++ (consider, readability of getSin(1)*getSin(2) or
computeSin(1)*computeSin(2) instead of just sin(1)*sin(2)), and,
arbitrary short forms such as "Elt" are just a way to add more work
when mispelinsg have to be corrected later on.

I can fix the problem putting all declarations in the class ... but i would
like to find a way to have structs declarations outside the class
MatrixCRS... is it possible ? is it a good idea?

There were workarounds for most things wrong with VC6, but unless you
absolutely have to support that compiler it's not a good idea to do that.
 
L

Lionel B

The following code don't raise even a warning with DevC++ but with VC6 :
Compiler Error C2908 : explicit specialization; 'template' has already
been specialized from the primary template

with the following code

(I've taken all comments out of the code because they're playing havoc
with my line-wrapping :-/)
template <typename T> struct Elem_
{
long x;
long y;
T el;
struct Elem_ * next_x;

You don't need 'struct' here...
struct Elem_ * next_y;

....or here
};

template <typename T> struct TableDisp_ {
struct Elem_<T> ** table;

....or here
long nbEntrees;
};


template <typename T> class MatrixCRS) {
typedef struct Elem_<T> Element;

....or here
public:
MatrixCRS() {};

MatrixCRS {}
MatrixCRS(T defaultValue);
~MatrixCRS();
T getDefaultElt();
private:
T m_default;
};

Compiles ok for me (apart from the undefined functions in MatrixCRS) with
gcc 4.2.0. I suspect VC6 is wrong (it is out of date and notoriously
dodgy on template support).
I can fix the problem

[...]

I think the problem is your compiler.
 
X

Xavier Serrand

Lionel B said:
The following code don't raise even a warning with DevC++ but with VC6 :
Compiler Error C2908 : explicit specialization; 'template' has already
been specialized from the primary template

with the following code

(I've taken all comments out of the code because they're playing havoc
with my line-wrapping :-/)
template <typename T> struct Elem_
{
long x;
long y;
T el;
struct Elem_ * next_x;

You don't need 'struct' here...
struct Elem_ * next_y;

...or here
};

template <typename T> struct TableDisp_ {
struct Elem_<T> ** table;

...or here
long nbEntrees;
};


template <typename T> class MatrixCRS) {
typedef struct Elem_<T> Element;

...or here
public:
MatrixCRS() {};

MatrixCRS {}
MatrixCRS(T defaultValue);
~MatrixCRS();
T getDefaultElt();
private:
T m_default;
};

Compiles ok for me (apart from the undefined functions in MatrixCRS) with
gcc 4.2.0. I suspect VC6 is wrong (it is out of date and notoriously
dodgy on template support).
I can fix the problem

[...]

I think the problem is your compiler.

Thanks Lionel !!

But... is it a difference, in C++, between

template <typename T> struct Elem_
{
long x;
long y;
T el;
Elem_ <T> * next_x;
Elem_ <T> * next_y;
}; // ==> no erros

and

template <typename T> struct Elem_
{
long x;
long y;
T el;
struct Elem_ <T> * next_x;
struct Elem_ <T> * next_y;
}; // ==> Compiler Error C2908 (with VC6)

does the C++ specification mention something on that topic ?
 
X

Xavier Serrand

Alf P. Steinbach said:
* Lionel B:

That correction is incorrect (and no correction needed).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


is it a difference, in C++, between

template <typename T> struct Elem_
{
long x;
long y;
T el;
Elem_ <T> * next_x;
Elem_ <T> * next_y;
}; // ==> no erros

and

template <typename T> struct Elem_
{
long x;
long y;
T el;
struct Elem_ <T> * next_x;
struct Elem_ <T> * next_y;
}; // ==> Compiler Error C2908 (with VC6)

does the C++ specification mention something on that topic ?
 
L

Lionel B

is it a difference, in C++, between

template <typename T> struct Elem_
{
long x;
long y;
T el;
Elem_ <T> * next_x;
Elem_ <T> * next_y;
}; // ==> no erros

and

template <typename T> struct Elem_
{
long x;
long y;
T el;
struct Elem_ <T> * next_x;
struct Elem_ <T> * next_y;
}; // ==> Compiler Error C2908 (with VC6)

does the C++ specification mention something on that topic ?

Not sure (g++ doesn't seem to mind either way). I think the 'struct' may
be mandatory in C, but optional (and redundant) in C++.
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top