473,575 Members | 3,347 Online
Bytes | Software Development & Data Engineering Community
+ 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 << "Overloadin g 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 << "Overloadin g 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 << "Overloadin g for const char[] \n"; }

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

/*
template <typename T, int N>
void func( T (&) [N] )
{ cout << "Overloadin g 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 1828
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<typena me 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.tug raz.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 << "Overloadin g 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 << "Overloadin g for const char[] \n"; }

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

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

template <typename T, int N>
void func( const T (&) [N] )
{ cout << "Overloadin g 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 << "Overloadin g 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.tug raz.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 << "Overloadin g 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 << "Overloadin g 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
"specialize d" 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 << "Overloadin g for T [] \n"; }

template <typename T, int N>
void func( const T (&) [N] )
{ cout << "Overloadin g 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
2599
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 the code below. The function template < typename T >
11
1974
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 distinguish between the types.
22
1858
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 to site B. Thanks in advance. Rich
6
5668
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 does the same in case of Explicit Specialization, so I either have to delcare specializations inline or move definitions to cpp file to avoid LNK2005...
1
1515
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, typename X::C) and specialise it in the case X takes some particular value, such as: >template<> typename Bar::A Foo<Bar>(typename Bar::B,...
2
5032
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 folder structure and naming conventions and then send a Net send message to those users telling them to rectify. The information I want to get is when...
1
1958
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 statement. Help.ShowHelp(Parent:=Me, url:=Me.HELP_URL_PRE & Me.myWorker.HelpFile) How do I get it to go away when the program exits? Now when I quit...
3
2442
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 template class. Here is what I would like to be able to call: ChannelIndexTraits<RGB>::index<RED>() == 0 ChannelIndexTraits<RGB>::index<GREEN>() ==...
30
2244
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)
0
7775
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8120
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
6515
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5664
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5338
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3778
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2286
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 we have to send another system
1
1382
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1107
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.