Partial specialization after instantiation, no error?

D

dascandy

The following program produces no warnings, no errors, but doesn't do
what I expect it to. I expect it to produce a warning or error. What
does the standard say about this and/or what should it say? Stroustrup
mentions it's an error (The C++ programming language, p343) but
doesn't say whether the standard says so or whether it's
implementation defined.

Both GCC and MSVC return "4 4 40", showing they instantiated the
template at the first oppurtunity and cached the instantiation.

#include <stdio.h>

template <typename T>
struct A {
T o;
};

int sizeofa = sizeof(A<int *>);

template <typename T>
struct A<T *> {
T o[5];
};

int newsizeofa = sizeof(A<int *>);

int main() {
printf("%d, %d %d\n", sizeofa, sizeof(A<int *>),
sizeof(A<double *>));
}

Shouldn't this produce a warning?
 
C

Colander

The following program produces no warnings, no errors, but doesn't do
what I expect it to. I expect it to produce a warning or error. What
does the standard say about this and/or what should it say? Stroustrup
mentions it's an error (The C++ programming language, p343) but
doesn't say whether the standard says so or whether it's
implementation defined.

Both GCC and MSVC return "4 4 40", showing they instantiated the
template at the first oppurtunity and cached the instantiation.

#include <stdio.h>

template <typename T>
struct A {
T o;

};

int sizeofa = sizeof(A<int *>);

template <typename T>
struct A<T *> {
T o[5];

};

int newsizeofa = sizeof(A<int *>);

int main() {
printf("%d, %d %d\n", sizeofa, sizeof(A<int *>),
sizeof(A<double *>));

}

Shouldn't this produce a warning?

Nope, it's an error :)

ComeauTest.c", line 12: error: this partial specialization would have
been used to
instantiate class "A<int *>"
struct A<T *> {
^

Look at:
http://www.comeaucomputing.com/tryitout/

Good luck
 
D

dasjotre

The following program produces no warnings, no errors, but doesn't do
what I expect it to. I expect it to produce a warning or error. What
does the standard say about this and/or what should it say? Stroustrup
mentions it's an error (The C++ programming language, p343) but
doesn't say whether the standard says so or whether it's
implementation defined.

Both GCC and MSVC return "4 4 40", showing they instantiated the
template at the first oppurtunity and cached the instantiation.

#include <stdio.h>

template <typename T>
struct A {
T o;

};

int sizeofa = sizeof(A<int *>);

template <typename T>
struct A<T *> {
T o[5];

};

int newsizeofa = sizeof(A<int *>);

int main() {
printf("%d, %d %d\n", sizeofa, sizeof(A<int *>),
sizeof(A<double *>));

}

Shouldn't this produce a warning?

standard says [14.7.3.7]
"When writing a specialization, be careful about
its location; or to make it compile will be such
a trial as to kindle its self-immolation."

with sizeof(A<int*>) you have explicitly instantiated
A for int * . since it comes before the specialization
for pointer types the compiler does its best.
second sizeof(A<int*>) doesn't consider pointer
type specialization since A<int*> has already been
instantiated.

since the first instantiation of A<double*> is after
pointer type specialization, compiler uses the
specialized version.

regards

DS
 
D

dasjotre

The following program produces no warnings, no errors, but doesn't do
what I expect it to. I expect it to produce a warning or error. What
does the standard say about this and/or what should it say? Stroustrup
mentions it's an error (The C++ programming language, p343) but
doesn't say whether the standard says so or whether it's
implementation defined.

Both GCC and MSVC return "4 4 40", showing they instantiated the
template at the first oppurtunity and cached the instantiation.

#include <stdio.h>

template <typename T>
struct A {
T o;

};

int sizeofa = sizeof(A<int *>);

template <typename T>
struct A<T *> {
T o[5];

};

int newsizeofa = sizeof(A<int *>);

int main() {
printf("%d, %d %d\n", sizeofa, sizeof(A<int *>),
sizeof(A<double *>));

}

Shouldn't this produce a warning?

14.7.3.7
"When writing a specialization, be careful
about its location; or to make it compile
will be such a trial as to kindle its
self-immolation."

basically, ordering of specializations and
instantiations is significant. some compilers
do it better than others to find best fit.

first sizeof(A<int*>) instantiates A<int*> and the
compiler does its best. second sizeof(A<int*>) doesn't
consider specialization because A<int*> has already
been instantiated.A<double*> is instantiated after
the specialization so the specialization is used.

regards

DS
 
K

Kai-Uwe Bux

Colander said:
The following program produces no warnings, no errors, but doesn't do
what I expect it to. I expect it to produce a warning or error. What
does the standard say about this and/or what should it say? Stroustrup
mentions it's an error (The C++ programming language, p343) but
doesn't say whether the standard says so or whether it's
implementation defined.

Both GCC and MSVC return "4 4 40", showing they instantiated the
template at the first oppurtunity and cached the instantiation.

#include <stdio.h>

template <typename T>
struct A {
T o;

};

int sizeofa = sizeof(A<int *>);

template <typename T>
struct A<T *> {
T o[5];

};

int newsizeofa = sizeof(A<int *>);

int main() {
printf("%d, %d %d\n", sizeofa, sizeof(A<int *>),
sizeof(A<double *>));

}

Shouldn't this produce a warning?

Nope,

Right. It does not need to trigger any diagnostic.
it's an error :)

Correct, too.

From the standard [14.7.3.6]

If a template, a member template or the member of a class template is
explicitly specialized then that specialization shall be declared before
the first use of that specialization that would cause an implicit
instantiation to take place, in every translation unit in which such a use
occurs; no diagnostic is required.
^^^^^^^^^^^^^^^^^^^^^^^^^

The good news is that a compiler is allowed to flag this error:
ComeauTest.c", line 12: error: this partial specialization would have
been used to
instantiate class "A<int *>"
struct A<T *> {
^


Best

Kai-Uwe Bux
 
D

Default User

dasjotre wrote:

standard says [14.7.3.7]
"When writing a specialization, be careful about
its location; or to make it compile will be such
a trial as to kindle its self-immolation."

Unusually poetic for a standard.




Brian
 

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
474,292
Messages
2,571,494
Members
48,174
Latest member
RonnyLoren

Latest Threads

Top