473,779 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Partial but not full/explicit specialization at non-namespace scope.

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> {};

// but this IS:
template <typename T2>
struct B<int, T2> {};
};

What is the reason for this rule?
Oct 19 '05 #1
8 4802

Ferdi Smit wrote:
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> {};

// but this IS:
template <typename T2>
struct B<int, T2> {};
};

What is the reason for this rule?


VC++ 7.1 the code below compiles

struct A {
template <typename T1, typename T2>
struct B {};

template <>
struct B<int, float> {};

template <typename T2>
struct B<int, T2> {};
};

int main()
{
A a;
A::B<char, char> b1;
A::B<int, float> b2;
A::B<int, char> b3;

return 0;
}

so I guess it is time to change compilers.

dan

Oct 19 '05 #2
Ferdi Smit wrote:
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> {};

// but this IS:
template <typename T2>
struct B<int, T2> {};
};

What is the reason for this rule?


I strongly recommend asking about rationales in comp.std.c++. Here we
talk the "how", there they discuss the "why".

V
Oct 19 '05 #3
In article <11************ **********@g43g 2000cwa.googleg roups.com>,
Dan Cernat <dc*****@excite .com> wrote:
Ferdi Smit wrote:
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> {};

// but this IS:
template <typename T2>
struct B<int, T2> {};
};

What is the reason for this rule?


VC++ 7.1 the code below compiles

struct A {
template <typename T1, typename T2>
struct B {};

template <>
struct B<int, float> {};

template <typename T2>
struct B<int, T2> {};
};

int main()
{
A a;
A::B<char, char> b1;
A::B<int, float> b2;
A::B<int, char> b3;

return 0;
}

so I guess it is time to change compilers.


Comeau C++ will also compile the above code.

However, it will only do it in our VC++ compatibility mode,
because in standard mode it is an error:

Comeau C/C++ 4.3.4.1 (Mar 30 2005 22:54:12) for MS_WINDOWS_x86
Copyright 1988-2005 Comeau Computing. All rights reserved.
MODE:strict errors C++

"it2.cpp", line 5: error: explicit specialization is not allowed in the
current scope
template <>
^

This is as per Standard C++, since that line can't be in class scope.

So I guess it is time to change compilers :)
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 19 '05 #4

Greg Comeau wrote:
In article <11************ **********@g43g 2000cwa.googleg roups.com>,
Dan Cernat <dc*****@excite .com> wrote:
Ferdi Smit wrote: [snip]

VC++ 7.1 the code below compiles
[snip again]
so I guess it is time to change compilers.


Comeau C++ will also compile the above code.

However, it will only do it in our VC++ compatibility mode,
because in standard mode it is an error:

Comeau C/C++ 4.3.4.1 (Mar 30 2005 22:54:12) for MS_WINDOWS_x86
Copyright 1988-2005 Comeau Computing. All rights reserved.
MODE:strict errors C++

"it2.cpp", line 5: error: explicit specialization is not allowed in the
current scope
template <>
^

This is as per Standard C++, since that line can't be in class scope.

So I guess it is time to change compilers :)


LOL

Thanks, Greg.

Oct 19 '05 #5
In article <11************ **********@g49g 2000cwa.googleg roups.com>,
Dan Cernat <dc*****@excite .com> wrote:

Greg Comeau wrote:
In article <11************ **********@g43g 2000cwa.googleg roups.com>,
Dan Cernat <dc*****@excite .com> wrote:
>Ferdi Smit wrote:[snip]
>
>VC++ 7.1 the code below compiles
> [snip again]
>so I guess it is time to change compilers.


Comeau C++ will also compile the above code.

However, it will only do it in our VC++ compatibility mode,
because in standard mode it is an error:

Comeau C/C++ 4.3.4.1 (Mar 30 2005 22:54:12) for MS_WINDOWS_x86
Copyright 1988-2005 Comeau Computing. All rights reserved.
MODE:strict errors C++

"it2.cpp", line 5: error: explicit specialization is not allowed in the
current scope
template <>
^

This is as per Standard C++, since that line can't be in class scope.

So I guess it is time to change compilers :)


LOL

Thanks, Greg.


BTW, the reason, at least as I recall it (so I could be wrong
and/or recalling it wrong), was "simply" that it was not known if
allowing it that way would be beneficial, and since allowing
a more liberal structure was considered to be a possible
implementation burden upon implementators, it was prohibited.
It's come up before about relaxing that, especially in combo
with other prohibitions.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 19 '05 #6
"Dan Cernat" <dc*****@excite .com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .


VC++ 7.1 the code below compiles
[snip]
so I guess it is time to change compilers.


Yes, I know, perhaps I ought to have mentioned that. Anyway, not trying to
bash anyone, but VC7.1 is not a very good compiler to test these matters
with. I've found numerous of similar, wrong "features" concerning templates;
even in strict mode. But thanks for your time in trying to compile it for
me, it's appreciated :)

--
Ferdi Smit
Oct 19 '05 #7
"Greg Comeau" <co****@panix.c om> wrote in message
news:dj******** **@panix3.panix .com...
In article <11************ **********@g49g 2000cwa.googleg roups.com>,
BTW, the reason, at least as I recall it (so I could be wrong
and/or recalling it wrong), was "simply" that it was not known if
allowing it that way would be beneficial, and since allowing
a more liberal structure was considered to be a possible
implementation burden upon implementators, it was prohibited.
It's come up before about relaxing that, especially in combo
with other prohibitions.


But isn't partial specialization the more liberal structure? Whenever I
really need explicit specialization in this way, I can simply add a dummy
template parameter :

struct A {
template <typename T, int dummy>
struct B {};

// this is ok, simulating explicit spec. with dummy
template <int dummy>
struct B<float, dummy> {};

// then why is this (or similar without the dummy) not ok?
template <>
struct B<float, 0> {};
};

I've used this to write templated recursion on an integer in the class. As
(member) functions can only be explicitly specialized, adding a dummy
template parameter is not an option there. Another way out is to use an
Int2Type construct (ala alexandrescu/loki) and function overloading...
however, why do I have to resort to these kinds of tricks? To me it doesn't
really make sense, however I might be missing a deeper issue (?)

--
Ferdi Smit
Oct 19 '05 #8
Ferdi Smit wrote:
"Greg Comeau" <co****@panix.c om> wrote in message
news:dj******** **@panix3.panix .com...
In article <11************ **********@g49g 2000cwa.googleg roups.com>,
BTW, the reason, at least as I recall it (so I could be wrong
and/or recalling it wrong), was "simply" that it was not known if
allowing it that way would be beneficial, and since allowing
a more liberal structure was considered to be a possible
implementation burden upon implementators, it was prohibited.
It's come up before about relaxing that, especially in combo
with other prohibitions.


But isn't partial specialization the more liberal structure? Whenever I
really need explicit specialization in this way, I can simply add a dummy
template parameter :

struct A {
template <typename T, int dummy>
struct B {};

// this is ok, simulating explicit spec. with dummy
template <int dummy>
struct B<float, dummy> {};

// then why is this (or similar without the dummy) not ok?
template <>
struct B<float, 0> {};
};

I've used this to write templated recursion on an integer in the class. As
(member) functions can only be explicitly specialized, adding a dummy
template parameter is not an option there. Another way out is to use an
Int2Type construct (ala alexandrescu/loki) and function overloading...
however, why do I have to resort to these kinds of tricks? To me it doesn't
really make sense, however I might be missing a deeper issue (?)


All that is missing is a namespace. Declaring A in a namespace lets B
be explicitly instantiated:

namespace N {

struct A
{
template <typename T1, typename T2>
struct B
{
};
};

// explicit instantiation for N::A::B<int, int>
template <>
struct A::B<int, float>
{
}; }

using N::A;

int main()
{
A::B<int, float> a;
}

Why A has to be in a namespace in order for B to be explicitly
instantiated is a bit unclear. But as this example shows, the effect on
the program is otherwise unlikely to be dramatic. And declaring classes
in non-global namespaces is usually a good idea for other reasons.

Greg

Oct 19 '05 #9

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

Similar topics

9
2546
by: Philip Lawatsch | last post by:
Hi I'd like to implement some kind if type traits myself, but I have to support broken compilers (like visual studio) that do not support Partial Specialization. My first shot was something like this: ----8<--- typedef char IsPODForListArrayTrue;
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
1
1650
by: SainTiss | last post by:
Hi, I've been looking into the standard for a clear statement on whether partial specialization of member functions of class templates is allowed or not. 14.7.3/4 says that explicit specialization of a member function is legal, but doesn't state that partial specialization is not. One might argue that the standard indicates that partial specialization implies a distinct template, and therefore defining a member function
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
1
2272
by: Alfonso Morra | last post by:
if I have a class template declared as ff: (BTW is this a partial specialization? - I think it is) template <typename T1, myenum_1 e1=OK, my_enum_2=NONE> class A { public: A(); virtual ~A() ;
2
3690
by: Michael Stembera | last post by:
Here is a very simple piece of code to repro this bug. template<typename T, int N> inline bool foo( void ) { return true; } template<typename T> inline bool foo<T, 1>( void ) { return false;
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
2784
by: Marek Vondrak | last post by:
Hello. I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second assign() function really a specialization of the first assign() or is it an assign() overload? Thank you. -- Marek
9
1963
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
0
9474
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,...
0
10306
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10074
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
9930
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...
0
8961
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5373
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...
0
5503
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2869
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.