473,398 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Partial specialization after instantiation, no error?

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?

Jun 27 '07 #1
5 5147
On Jun 27, 2:52 pm, "dasca...@gmail.com" <dasca...@gmail.comwrote:
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

Jun 27 '07 #2
On 27 Jun, 13:52, "dasca...@gmail.com" <dasca...@gmail.comwrote:
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

Jun 27 '07 #3
On 27 Jun, 14:50, dasjotre <dasjo...@googlemail.comwrote:
sorry for duplicate, darn google groups.

Jun 27 '07 #4
Colander wrote:
On Jun 27, 2:52 pm, "dasca...@gmail.com" <dasca...@gmail.comwrote:
>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
Jun 27 '07 #5
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
Jun 27 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section...
8
by: Agent Mulder | last post by:
Hi group, I have a problem with partial template specialization. In the code below I have a template struct Music with one method, play(), and three kinds of music, Jazz, Funk and Bach. When I...
2
by: Shekhar | last post by:
template<typename T> struct A{}; //line 1 template<typename T> struct B{}; //line 2 template<typename T> struct B<A<T> > {}; //line 3: partial specialization of B VC6.0 compiler results for the...
5
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
8
by: Ferdi Smit | last post by:
I've never understood the rationale of allowing partial, but not explicit specialization for classes at non-namespace scope. Ie.: struct A { template <typename T1, typename T2> struct B {}; ...
4
by: Alfonso Morra | last post by:
Does VC 7.1 support template specialization and partial specialization ?
4
by: wakun | last post by:
Hi there, I am learning template programming. When testing the partial specialization, I have some probelms Here is a full templated class template <typename T, int n> class CT { public: T...
9
by: Greg | last post by:
Hi, I would like to specify behavior of a class member relatively to template implemetation. It works in usual cases but it seems to fail with to templates when one of the two is specified... ...
1
by: Ioannis Gyftos | last post by:
Hello, First the code :) /////////////////////////////////////////////////////////////////////////////////// // in another header file namespace LJC{
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.