473,769 Members | 8,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5195
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...@googl email.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
implementati on 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<doubl e *>));

}

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
6865
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 18.3.1, 'Iseq'). I want the version for containers that he gives, but also to provide a specialization for construction from a pair<It,It> (eg because that is returned by equal_range()).
8
7689
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 specialize Music<Bach>, I expect that the original play() method is available in the specialization, but it is not. How can I fix this? -X
2
1508
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 above: at line 3: error C2989: 'B<struct A<T> >' : template class has already been defined as a non-template class
5
6589
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
4802
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 {}; // this is not allowed: template <> struct B<int, float> {};
4
1856
by: Alfonso Morra | last post by:
Does VC 7.1 support template specialization and partial specialization ?
4
1501
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 data;
9
1962
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... Exemple: template <class T, int Num> class A
1
2228
by: Ioannis Gyftos | last post by:
Hello, First the code :) /////////////////////////////////////////////////////////////////////////////////// // in another header file namespace LJC{
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9998
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7413
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3967
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3567
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.