Hello,
I think I have found a template specialization bug in Microsoft Visual
C++ 2003 (VC7.1). The following code compiles with gcc3.2 and Intel
8.0, but gives error C2764 "template parameter not used in partial
specialization" with VC7.1.

The workaround would be to repeat the complex type deduction done in
DefinesAType for the specialization of SomeClass, which is code
duplication and error-prone.

//a class that has public typedef
template
< typename T >
struct DefinesAType
{
//assembles in a complicated (assumed)
//way a new type from T
typedef T AType;
};

//class SomeClass will be specialized
template
< class T>
class
SomeClass
{};

//specialize on the type defined in DefinesAType
template
< class T >
class
SomeClass<typename DefinesAType<T>::AType>
{
};


int main()
{
SomeClass<int> sc;
}

Re: template specialization bug by Hendrik

Hendrik
Mon Mar 01 13:26:03 CST 2004

Martin Wartens <martin.wartens@my-mail.ch> wrote:
> Hello,
> I think I have found a template specialization bug in Microsoft Visual
> C++ 2003 (VC7.1). [...]

FWIW, como agress with you:

Comeau C/C++ 4.3.3 (Aug 6 2003 15:13:37) for ONLINE_EVALUATION_BETA1
Copyright 1988-2003 Comeau Computing. All rights reserved.
MODE:strict errors C++

"ComeauTest.c", line 29: warning: variable "sc" was declared but never referenced
SomeClass<int> sc;
^

> [...]

Schobi

--
SpamTrap@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers



Re: template specialization bug by Bonj

Bonj
Mon Mar 01 14:07:43 CST 2004

What's the purpose of this code?


"Martin Wartens" <martin.wartens@my-mail.ch> wrote in message
news:57c9e1d0.0403010854.7337a8e9@posting.google.com...
> Hello,
> I think I have found a template specialization bug in Microsoft Visual
> C++ 2003 (VC7.1). The following code compiles with gcc3.2 and Intel
> 8.0, but gives error C2764 "template parameter not used in partial
> specialization" with VC7.1.
>
> The workaround would be to repeat the complex type deduction done in
> DefinesAType for the specialization of SomeClass, which is code
> duplication and error-prone.
>
> //a class that has public typedef
> template
> < typename T >
> struct DefinesAType
> {
> //assembles in a complicated (assumed)
> //way a new type from T
> typedef T AType;
> };
>
> //class SomeClass will be specialized
> template
> < class T>
> class
> SomeClass
> {};
>
> //specialize on the type defined in DefinesAType
> template
> < class T >
> class
> SomeClass<typename DefinesAType<T>::AType>
> {
> };
>
>
> int main()
> {
> SomeClass<int> sc;
> }



Re: template specialization bug by martin

martin
Tue Mar 02 04:29:58 CST 2004

I forgot to mention the line that causes the error
C2764 "template parameter T not used in partial specialization" with
VC7.1
is here:
SomeClass<typename DefinesAType<T>::AType>

I suppose this bug/missing feature is already known, but I found
nothing about it.

Originally, I wanted to use this kind of construct to specialize a
std::less functor for a type that I had assembled in a type traits
class.

[Lengthy explanation follows: ]
Say you want to use a container to store objects of type LargeType.
For several reasons, you want to store pointers to LargeType (large
objects, shared objects). To provide a meaningful sort, you need a
less operator that dereferences the pointers before comparison, such
that the objects are compared and not only the pointers. So you have
to provide a specialized std::less for this case.

A type traits class contains information about a certain type. It
collects types and constants. Inside the class you can do type
calculations like removing reference and const from at type. Other
classes use the TraitsClass to access type information.

Type traits specialization for LargeType:
(imagine this to be more complicated)

template<>
struct TraitsClass<LargeType>
{
typedef LargeType* Ptr_T;
}

No we want to automagically specify a std::less for pointers for all
Types that match LargeType in the specialization above. This works as
follows: The compiler tries to match TraitsClass for class T, and if
it finds a match extracts the Ptr_T. std::less will then be
instantiated with this pointer type. If there is no match, nothing
happens, the default std::less will be used.

template
< typename T>
struct std::less<typename TraitsClass<T>::Ptr_T>
{
//dereference pointers, then compare
}

The advantage of this construct is that we can gather the type
informations and type computations in one place (the traits class) and
don't have to duplicate them.

Re: template specialization bug by David

David
Tue Mar 02 05:11:48 CST 2004

>I think I have found a template specialization bug in Microsoft Visual
>C++ 2003 (VC7.1). The following code compiles with gcc3.2 and Intel
>8.0, but gives error C2764 "template parameter not used in partial
>specialization" with VC7.1.

Martin,

FWIW, I've passed your example on to MS since it's still a problem
with the current Whidbey alpha compiler.

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq