473,503 Members | 1,813 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 4779

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**********************@g43g2000cwa.googlegroups .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**********************@g43g2000cwa.googlegroups .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**********************@g49g2000cwa.googlegroups .com>,
Dan Cernat <dc*****@excite.com> wrote:

Greg Comeau wrote:
In article <11**********************@g43g2000cwa.googlegroups .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.googlegr oups.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.com> wrote in message
news:dj**********@panix3.panix.com...
In article <11**********************@g49g2000cwa.googlegroups .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.com> wrote in message
news:dj**********@panix3.panix.com...
In article <11**********************@g49g2000cwa.googlegroups .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
2523
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...
8
7649
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...
1
1626
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...
2
1493
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...
1
2254
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...
2
3666
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...
4
1836
by: Alfonso Morra | last post by:
Does VC 7.1 support template specialization and partial specialization ?
4
1485
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
2742
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...
9
1934
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... ...
0
7202
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
7280
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
7330
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...
0
5578
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,...
1
5014
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...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
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 ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.