Blitz logo

Blitz Support :

From: Julian C. Cummings (cummings_at_[hidden])
Date: 2004-06-14 16:42:13


Hi Carlos,

> My question was more a general one, in the sense of, why does
> this not work in VC++ while it does in g++. Is this non
> standard use of the templates? In VC++ the problem is not
> that it can't find the templates, it that it refuses to
> recognise the specialisations as specialisations of that
> template, the error it gives is:
>
> c:\Documents and Settings\crega\My Documents\Visual Studio
> Projects\templateTest\templateTest.cpp(16) : error C2912:
> explicit specialization; 'void
> testWrapper<float>(MKL_Traits<T>::mType *,int *)' is not a
> specialization of a function template
>
> with
>
> [
>
> T=float
>
> ]
>
> g++ has no problem with it.

I don't have a definitive answer for you on this. I checked that the Intel
C++ compiler also accepts this code in strict ANSI mode, so I tend to think
that it is proper code and that the MS VC++ compiler is simply in error.
However, this form of function template specialization disturbs me because
of the fact that the template argument matching cannot be uniquely
determined. It is legal to have function templates where not all the
template arguments can be determined by the function signature. But I'm not
sure if that rule also applies to function templates where there is more
than one possible argument match. In this case, a complicated set of rules
is used to determine the best match. The trouble here is that the compiler
cannot "reverse engineer" the MKL_Traits class to figure out what T should
be in order for MKL_Traits<T>::mType to match the given type.

In playing around with this example, I found another approach that is
accepted by the MS VC++ compiler. You can write the testWrapper function
template like this:

template <class T>
void testWrapper(T *,int *);

template <>
void testWrapper(MKL_Traits<float>::mType *,int *);

template <>
void testWrapper(MKL_Traits<std::complex<float> >::mType *,int *);

With this approach, T matches the resulting type exported by MKL_Traits, so
there is no issue of non-unique argument matching, and the type of T is
clear to the compiler.

> As you said, the templates don't really add any advantage
> over overloading, I checked the generated assembly over the
> weekend, and I am going with overloading, but I am still
> curious as to why my code did not work with visual studio.

OK, good to have this sanity check. I would be shocked if this were not the
case.

Regards, Julian C.

Dr. Julian C. Cummings
Staff Scientist, CACR/Caltech
(626) 395-2543
cummings_at_[hidden]