469,625 Members | 1,775 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,625 developers. It's quick & easy.

Help: How many explicit specializations required?

I would be grateful if someone could point out if I am understanding
correctly and suggest ways to improve. Sorry for the long message and
I hope you will kindly bear with it. I have to make it elaborate to
make sure my questions are clear enough.

Let's say I need to write a function whose logic is same for all types
(T) except in the case of T * (including const T *). Furtheremore ,
the function needs to be written differently for character
strings(C-style).

I have tried various combination of explicit specialization and
overloading and came to the following conclusion:

1. I need to have a template for T
2. I need to overload the template for T *
3. I need to have explicit specialization or a normal function for
char *
4. I need to have explicit specialization or a normal function for
const char *

I tried to make 3 & 4 into one (hoping const char * will accept char
*), but I learned it cannot be when 1 or 2 is present since char *
will be instantiated from the template.

My questions are:

A. Since their logic will be exactly same, is there a way to make 3
and 4 into one function?

B. Passing arrays of T will be resolved into case 2. What if I don't
wish to treat arrays of T as pointers to T? Let's say the logic of
working on pointers to T will give unexpected result when worked on
arrays of T. Is there any way to treat arrays of T differently from
pointers to T?

C. The primary template passes by value. Can I make it
pass-by-const-reference?
Actually, I made my attempt at this and needs to know if I am doing
right.

Here is a simple program I wrote to help me understand what's going
on:

#include <iostream>
using std::cout;

template <typename T>
void func(T)
{ cout << "primary\n" ; }

template <typename T>
void func(T *) // should handle T * as well as const T *, I think
{ cout << "Overloading for Pointers\n"; }

template <>
void func(char *)
// BTW, Writing above as func<char *> makes gcc behave differently
{ cout << "specialization: char * \n"; }

template <>
void func(const char *)
// BTW, Writing above as func<const char *> makes gcc behave
differently
{ cout << "specialization: const char * \n"; }

int main()
{
char * pc = "whatever";
const char * pcc = "whatever";
char ac[] = "whatever";
const char acc[] = "whatever";
int i;
const int ci=1;
int ai[] = { 0 };
const int aci[] = {0};

cout << "int: "; func(i);
cout << "int *: "; func(&i);
cout << "const int: "; func(ci);
cout << "const int *: "; func(&ci);
cout << "int [] : "; func(ai);
cout << "const int [] :"; func(aci);

cout << "Literal String: "; func("whatever");
cout << "char *: "; func(pc);
cout << "const char *: "; func(pcc);
cout << "char []: "; func(ac);
cout << "const char[]: "; func(acc);
}

As asked in question C, can I make the primary function template pass
by const-reference to T? Here is my attempt but it looks rather
horrible as I need to provide the four different explicit
specializations whose internal logics are exactly same for handling
C-style strings. And I think this would also create code bloat as
different instantiations will be made for array of char of different
sizes.

Please have a look and give me some advice if I can make those four
separate explicit specialization combined into one (maybe two?)

#include <iostream>
using std::cout;

template <typename T>
void func(const T &)
{ cout << "primary\n" ; }

template <typename T>
void func(T * const &)
{ cout << "Overloading for Pointers\n"; }

template <>
void func(char * const &)
{ cout << "specialization: char * \n"; }

template <>
void func(const char * const &)
{ cout << "specialization: const char * \n"; }

template <int N>
void func( const char (&) [N] )
{ cout << "Overloading for const char[] \n"; }

template <int N>
void func( char (&) [N] )
{ cout << "Overloading for char[] \n"; }

/*
template <typename T, int N>
void func( T (&) [N] )
{ cout << "Overloading for T [] \n"; }
*/

// Including this creates ambiguity for arrays of T
// as it collides with the primary one.
int main()
{
char * pc = "whatever";
const char * pcc = "whatever";
char ac[] = "whatever";
const char acc[] = "whatever";
int i;
const int ci=1;
int ai[] = { 0 };
const int aci[] = {0};

cout << "int: "; func(i);
cout << "int *: "; func(&i);
cout << "const int: "; func(ci);
cout << "const int *: "; func(&ci);
cout << "int [] : "; func(ai);
cout << "const int [] :"; func(aci);

cout << "Literal String: "; func("whatever");
cout << "char *: "; func(pc);
cout << "const char *: "; func(pcc);
cout << "char []: "; func(ac);
cout << "const char []: "; func(acc);
}

Question B.2: Now, arrays of T are resolved into primary template. Is
there any way to differentiate between T and array of T?

Thank you for your patience, time and kind advice in advance.
Jul 22 '05 #1
4 1642
co******@yahoo.co.uk (CoolPint) writes:
I would be grateful if someone could point out if I am understanding
correctly and suggest ways to improve. Sorry for the long message and
I hope you will kindly bear with it. I have to make it elaborate to
make sure my questions are clear enough.

Let's say I need to write a function whose logic is same for all types
(T) except in the case of T * (including const T *). Furtheremore ,
the function needs to be written differently for character
strings(C-style).

I have tried various combination of explicit specialization and
overloading and came to the following conclusion:

1. I need to have a template for T
2. I need to overload the template for T *
3. I need to have explicit specialization or a normal function for
char *
4. I need to have explicit specialization or a normal function for
const char *


Maybe this helps:
Use typetraits! They are checking at compiletime which type of type T
is, for example if it's an pointer if it's a char and so on.
Now you can do this:

// only pseudo code
template<typename T>
void myFunc(T param)
{
if(typetraits<T>::is_pointer && typetraits<T>::is_char)
//do the stuff with char*
if(typetraits<T>::is_pointer && typetraits<T>::is_char) &&
typetraits<T>::is_const)
// do stuf with const char*
if(typetraits<T>::is_pointer)
// do stuff with pointer
else
// do stuff with the rest
}

On http://sourceforge.net/projects/loki-lib/ you can download freely the
library of Andrei Alexandrescu which includes a type traits template.

HTH
Nicolas

--
| Nicolas Pavlidis | Elvis Presly: |\ |__ |
| Student of SE & KM | "Into the goto" | \|__| |
| pa****@sbox.tugraz.at | ICQ #320057056 | |
|-------------------University of Technology, Graz----------------|
Jul 22 '05 #2
I just figured out how to differentiate between T and array of T. Not
only that, I also learned I had to differentiate between array of T
and array of const T, too.

Now, that involves a lot of specialization. I am now a bit clearer
about my originial questions B and C, but I still wonder about
combining the four separate specializations required for handing
C-style char strings.

I still very much love to hear ideas from experts. Thank you again.

Below is the program I modified from the previous post to show me
what's going on.

#include <iostream>
using std::cout;

template <typename T>
void func(const T &)
{ cout << "primary\n" ; }

template <typename T>
void func(T * const &) // should handle T * as well as const T *
{ cout << "Overloading for Pointers\n"; }

template <>
void func(char * const &)
{ cout << "specialization: char * \n"; }

template <>
void func(const char * const &)
{ cout << "specialization: const char * \n"; }

template <int N>
void func( const char (&) [N] )
{ cout << "Overloading for const char[] \n"; }

template <int N>
void func( char (&) [N] )
{ cout << "Overloading for char[] \n"; }

template <typename T, int N>
void func( T (&) [N] )
{ cout << "Overloading for T [] \n"; }

template <typename T, int N>
void func( const T (&) [N] )
{ cout << "Overloading for const T [] \n"; }

int main()
{
char * pc = "whatever";
const char * pcc = "whatever";
char ac[] = "whatever";
const char acc[] = "whatever";
int i;
const int ci=1;
int ai[] = { 0 };
const int aci[] = {0};

cout << "int: "; func(i);
cout << "int *: "; func(&i);
cout << "const int: "; func(ci);
cout << "const int *: "; func(&ci);
cout << "int [] : "; func(ai);
cout << "const int [] :"; func(aci);

cout << "Literal String: "; func("whatever");
cout << "char *: "; func(pc);
cout << "const char *: "; func(pcc);
cout << "char []: "; func(ac);
cout << "const char []: "; func(acc);
}
Jul 22 '05 #3
co******@yahoo.co.uk (CoolPint) writes:
template <typename T>
void func(T * const &) // should handle T * as well as const T *
{ cout << "Overloading for Pointers\n"; }

template <>
void func(char * const &)
{ cout << "specialization: char * \n"; }


This (taken as an example) will not work, spezialization (explicit or
partial) is only possible with clases.
There are two ways you can work around this:
1) Write a class where the function you need is a publc static member,
then you can do this what you want
2) Work with overloadings, this should work fine, because template and
non template functions are overloadable

Or you can still use typetrais and overload the function too for the
spezial int cases which you mentioned, which is IMHO the slightest
effort to reach your aim.

Kind regards,
Nicolas

--
| Nicolas Pavlidis | Elvis Presly: |\ |__ |
| Student of SE & KM | "Into the goto" | \|__| |
| pa****@sbox.tugraz.at | ICQ #320057056 | |
|-------------------University of Technology, Graz----------------|
Jul 22 '05 #4
> > template <typename T>
void func(T * const &) // should handle T * as well as const T *
{ cout << "Overloading for Pointers\n"; }

template <>
void func(char * const &)
{ cout << "specialization: char * \n"; }
This (taken as an example) will not work, spezialization (explicit or
partial) is only possible with clases.
There are two ways you can work around this:
1) Write a class where the function you need is a publc static member,
then you can do this what you want
2) Work with overloadings, this should work fine, because template and
non template functions are overloadable


Now you raised an issue I am quite confused about. I read in books
that there cannot be partial specialization of function templates, but
explicit specializations of function templates are allowoed.
template <>
void func(char * const &)
{ cout << "specialization: char * \n"; }
According to the books, the above seems to be a valid explicit
specialization of the primary template. But you seems to suggest
that's not the case. I am confused with the terms. So is the above
case of explicit specialization or overloading?

I read that function templates cannot have partial specialization but
there is something called partial ordering whose evaluation method is
just like partial specialization of class templates.
template <typename T>
void func(T * const &) // should handle T * as well as const T *
{ cout << "Overloading for Pointers\n"; }


I think the above case is a partial ordering (overloading of the
primary template with another template?) or is it? I am quite
confused.
What I understand is that because above overloaded template is more
"specialized" than the primary template, this template will be used to
instantiate a function when the argument is T *, then the resulting
instantiation will be entered into the list of candidate functions. Am
I understanding correctly?

How about the two below? Are they partial ordering of the primary
(overloading of templates with another templates)?

template <typename T, int N>
void func( T (&) [N] )
{ cout << "Overloading for T [] \n"; }

template <typename T, int N>
void func( const T (&) [N] )
{ cout << "Overloading for const T [] \n"; }

When I have a number of function templates whose names are same, will
compilers try to select only one of them from which to make a
instantiation?
Or will compilers make several instantiations from differente
templates and enter them all into the list of candidate functions?
Or you can still use typetrais and overload the function too for the
spezial int cases which you mentioned, which is IMHO the slightest
effort to reach your aim.


Thank you for pointing this out. I just started reading about traits
and it seems a wonderful idea. Thanks again.
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Gernot Frisch | last post: by
22 posts views Thread by Rich | last post: by
1 post views Thread by Thomas Barnet-Lamb | last post: by
1 post views Thread by Michael D. Reed | last post: by
3 posts views Thread by stain | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.