473,699 Members | 3,180 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 1841
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
2605
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
1986
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
1872
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
5672
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 ("already defined") errors. Why is that?
1
1523
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, typename Bar::C)
2
5042
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 you select the file/folder and then: Properties -> Security Tab -> Advanced Button -> Owner Tab ->...
1
1972
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 the program that I called it form the help file is sill displayed. Is there a way to get a handle...
3
2460
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>() == 1 ChannelIndexTraits<RGB>::index<ALPHA>() == -1
30
2254
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
8706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8633
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9199
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9055
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7787
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4392
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3076
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
2
2366
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.