Topic: how does template func specialization match primary template


Author: Graeme Prentice <gp1@paradise.net.nz>
Date: Sat, 13 Jul 2002 14:34:44 GMT
Raw View
For the following code I get an error with the Comeau compiler saying
that there is no primary template to match the specialization  - the
primary template takes a pointer to const and the specialization takes
a pointer to non const


template<typename T>
T f1( const T * begin) {
   return *begin;
}

int k1;

template <>
int f1( int * begin) {         // error here
   return k1 = *begin;
}

int main()
{
    int k1;
    f1(&k1);
    float v1;
    f1(&v1);
}

If I change the specialization to
template <>
int f1( const int * begin) {

it compiles ok.  What are the rules for matching here  - if argument
deduction is used then it succeeds because for a parameter of type
pointer, the CV qualification of the pointed to type does not have to
be the same   ( 14.8.2.1 para 3 bullet 2 )

hence the call of f1(&v1) is ok in main()

Whereabouts does the standard specify how the specialization is
matched to the primary template?  I know there's a defect report that
says partial ordering is used to select one of multiple viable primary
templates candidates but how is it determined which are viable.

Graeme

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "John Gilson" <jag@acm.org>
Date: Sun, 14 Jul 2002 05:47:28 GMT
Raw View
I'm not sure where the standard describes this, but a function template
explicit specialization is meant to take the place of a function template
for specific types and/or values that parameterize that function template.
So to specialize f1(), you need to define a function whose signature can
"unify" with it.

For

template<>
int f1( int *);

this unification can't occur but for

template<>
int f1(const int *);

it can unify with type parameter T in function template f1() being assigned
the type int.

Note that the following works

template<typename T>
T f(T *);

template<>
const int f(const int *);

with T being unified to const int.

jag

"Graeme Prentice" <gp1@paradise.net.nz> wrote in message
news:p89viu037r7h3cg4cbhjrkrm5u4u9lorul@4ax.com...
>
> For the following code I get an error with the Comeau compiler saying
> that there is no primary template to match the specialization  - the
> primary template takes a pointer to const and the specialization takes
> a pointer to non const
>
>
> template<typename T>
> T f1( const T * begin) {
>    return *begin;
> }
>
> int k1;
>
> template <>
> int f1( int * begin) {         // error here
>    return k1 = *begin;
> }
>
> int main()
> {
>     int k1;
>     f1(&k1);
>     float v1;
>     f1(&v1);
> }
>
> If I change the specialization to
> template <>
> int f1( const int * begin) {
>
> it compiles ok.  What are the rules for matching here  - if argument
> deduction is used then it succeeds because for a parameter of type
> pointer, the CV qualification of the pointed to type does not have to
> be the same   ( 14.8.2.1 para 3 bullet 2 )
>
> hence the call of f1(&v1) is ok in main()
>
> Whereabouts does the standard specify how the specialization is
> matched to the primary template?  I know there's a defect report that
> says partial ordering is used to select one of multiple viable primary
> templates candidates but how is it determined which are viable.
>
> Graeme
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]
>

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]