473,387 Members | 1,516 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,387 software developers and data experts.

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 4765

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
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
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
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
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
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
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
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: 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
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...

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.