473,466 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1814
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: C. Carbonera | last post by:
/* Hi, I have a problem with explicit instantiation of templates in Visual C++ 6.0. I have provided the source below. I have an example of a function template that produces incorrect output in...
11
by: Gernot Frisch | last post by:
// variable creation object template <class T> class CL { public: CL (const T& t) {} template <> CL<double>(const T& t) {} }; How do I make a constructor for T = double, ..., so I can...
22
by: Rich | last post by:
I am trying to create a site that will re-direct a user based on their OS. For this site I will need to send NT4 and 95 users to site A and 2000/XP users to site B. All others should be directed...
6
by: Vyacheslav Lanovets | last post by:
Hello, All! I know that Explicit Instantiation actually emits code to obj files (so you can even export them from the module as plain functions or classes). But I found that MSVC7.1 compiler...
1
by: Thomas Barnet-Lamb | last post by:
I have a query concerning the handling of explicit specializations. Essentially, I want to know whether it is legal to take a template like >template<class X> typename X::A Foo(typename X::B,...
2
by: John Regan | last post by:
Hello All I am trying to find the owner of a file or folder on our network (Windows 2000 Server) using VB.Net and/or API. so I can search for Folders that don't follow our company's specified...
1
by: Michael D. Reed | last post by:
I am using the help class to display a simple help file. I generated the help file using Word and saving it as a single page Web page (.mht extension). I show the help file with the following...
3
by: stain | last post by:
hi, the following code produces an "illegal explicit template specialization" warning. I have been fiddling around a while to find a solution to specialize a template method of a specialized...
30
by: Anarki | last post by:
The following is the program i am trying to compile //restrict.c #include <stdio.h> int main() { char arr = "Qualifiers" char * restrict p = arr; int i = 0; for(; i < 10; ++i)
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.