473,395 Members | 1,692 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Template Specialisation

Is it possible to force the compiler to use a generic template rather
than a
matching specialisation?

Consider the following:

////////////////////////////////////////// template.cpp starts here

#include <iostream>
#include <ostream>
template<class T>
void f(T t)
{
std::cout << "template<Tvoid f(T t) called: t == " << t
<< std::endl;
}
template<void f<int>(int i)
{
std::cout << "specialisation void f<int>(int i) called: i == "
<< i << std::endl;
}

void f(int j){
std::cout << "overloaded void f(int j) called: j == "
<< j << std::endl;
}

int main()
{
f(1); // simple function matches before template
f<int>(2); // specialisation
f<>(3); // no need to spell out <int>, because it *must* be int
f('4'); // template matches - f<char>(char)
return 0;
}

////////////////////////////////////////// template.cpp ends here

Is there a way to call f with an int parameter so that the generic
f<T>
is called instead of the f<intspecialisation?

I don't want to do this, and I cannot think of a reason for doing it.
I'm just wondering.
Nov 23 '07 #1
6 1611
jo**********@hotmail.com wrote:
Is there a way to call f with an int parameter so that the generic
f<Tis called instead of the f<intspecialisation?
There is no f<T>, a template is kind of a compiler macro, the compiler
generates functions only if you instantiate a template (expand the
macro, in other words).
Nov 23 '07 #2
On Nov 23, 9:49 am, Matthias Buelow <m...@incubus.dewrote:
johnbrown...@hotmail.com wrote:
Is there a way to call f with an int parameter so that the generic
f<Tis called instead of the f<intspecialisation?

There is no f<T>, a template is kind of a compiler macro, the compiler
generates functions only if you instantiate a template (expand the
macro, in other words).
I understand that. Consider the output of the program that I posted:

overloaded void f(int j) called: j == 1
specialization void f<int>(int i) called: i == 2
specialization void f<int>(int i) called: i == 3
template<Tvoid f(T t) called: t == 4

Is there a way to write a call to f(5), so that the output would be:
template<Tvoid f(T t) called: t == 5
Never mind the semantic accuracy or elegance (or lack thereof!) of
what I wrote. If this is what we are aiming for, I suppose the
question
would be:

Is there a way to make the compiler generate its own instantiation,
rather than use the explicit one that I provided? When asked this way,
it would seem that the answer would be no, because if it generated its
own instantiation, it would be a duplicate symbol, because mine
already
exists.

Still, one never knows if the Powers That Be thought of some clever
reason to allow this, that mere mortals such as myself could not.
That's why I'm asking.
Nov 23 '07 #3
On Fri, 23 Nov 2007 06:39:05 -0800, johnbrown105 wrote:
Is it possible to force the compiler to use a generic template rather
than a
matching specialisation?

Consider the following:

////////////////////////////////////////// template.cpp starts here

#include <iostream>
#include <ostream>
template<class T>
void f(T t)
{
std::cout << "template<Tvoid f(T t) called: t == " << t
<< std::endl;
}
template<void f<int>(int i)
{
std::cout << "specialisation void f<int>(int i) called: i == "
<< i << std::endl;
}

void f(int j){
std::cout << "overloaded void f(int j) called: j == "
<< j << std::endl;
}

int main()
{
f(1); // simple function matches before template f<int>(2);
// specialisation
f<>(3); // no need to spell out <int>, because it *must* be int
f('4'); // template matches - f<char>(char) return 0;
}

////////////////////////////////////////// template.cpp ends here

Is there a way to call f with an int parameter so that the generic f<T>
is called instead of the f<intspecialisation?
f(3u) or f<unsigned>(3) would be OK?

--
Tadeusz B. Kopec (tk****@NOSPAMPLEASElife.pl)
Great minds run in great circles.
Nov 23 '07 #4

<jo**********@hotmail.comwrote in message news:55**********************************@i12g2000 prf.googlegroups.com...
On Nov 23, 9:49 am, Matthias Buelow <m...@incubus.dewrote:
>johnbrown...@hotmail.com wrote:
Is there a way to call f with an int parameter so that the generic
f<Tis called instead of the f<intspecialisation?

There is no f<T>, a template is kind of a compiler macro, the compiler
generates functions only if you instantiate a template (expand the
macro, in other words).

I understand that. Consider the output of the program that I posted:

overloaded void f(int j) called: j == 1
specialization void f<int>(int i) called: i == 2
specialization void f<int>(int i) called: i == 3
template<Tvoid f(T t) called: t == 4

Is there a way to write a call to f(5), so that the output would be:
template<Tvoid f(T t) called: t == 5
Never mind the semantic accuracy or elegance (or lack thereof!) of
what I wrote. If this is what we are aiming for, I suppose the
question
would be:

Is there a way to make the compiler generate its own instantiation,
rather than use the explicit one that I provided? When asked this way,
it would seem that the answer would be no, because if it generated its
own instantiation, it would be a duplicate symbol, because mine
already
exists.

Still, one never knows if the Powers That Be thought of some clever
reason to allow this, that mere mortals such as myself could not.
That's why I'm asking.
Now suppose you have some sort of container class called
dweeblist that is filled with dweeb objects. A pointer to the
current dweeb object is produced with member function current().
You can define a function that applies a member function
(which is passed as an argument) to all dweeb objects in dweeblist
and then call this function for dweeblist dl like this:

#include <iostream.h>

class dweeb {
public:
void spin () {cout << "spinning!" << endl; }
void dodge () {cout << "dodging!" << endl; }
void feint () {cout << "feinting!" << endl; }
};

template<int szclass dweeblist {
dweeb list [sz];
int cursor;
public:
dweeblist () : cursor(0) {}
void reset () { cursor = 0; }
dweeb * current () { return &list [cursor]; }
void next () { cursor++; }
int end () { return cursor >= sz; }
void apply (void (dweeb::* df) () ) {
reset ();
while ( !end() ) {
(current ()->*df) ();
next ();
}
}
};

void main () {
dweeblist<7dl;
dl.apply(&dweeb::spin);
dl.apply(&dweeb::dodge);
dl.apply(&dweeb::feint);
}

Typically, the kind of function you'd want to apply()
to every object in a list would be something like draw().
for CAD.

Pointers to members have fairly limited use, but they
can be quite helpful when you want to delay the selection
of a specific function until the program is running.

C++ Inside & Out
Bruce Eckel
Nov 23 '07 #5
On Nov 23, 12:51 pm, "Tadeusz B. Kopec" <tko...@NOSPAMPLEASElife.pl>
wrote:
On Fri, 23 Nov 2007 06:39:05 -0800, johnbrown105 wrote:
Is it possible to force the compiler to use a generic template rather
than a
matching specialisation?
Consider the following:
////////////////////////////////////////// template.cpp starts here
#include <iostream>
#include <ostream>
template<class T>
void f(T t)
{
std::cout << "template<Tvoid f(T t) called: t == " << t
<< std::endl;
}
template<void f<int>(int i)
{
std::cout << "specialisation void f<int>(int i) called: i == "
<< i << std::endl;
}
void f(int j){
std::cout << "overloaded void f(int j) called: j == "
<< j << std::endl;
}
int main()
{
f(1); // simple function matches before template f<int>(2);
// specialisation
f<>(3); // no need to spell out <int>, because it *must* be int
f('4'); // template matches - f<char>(char) return 0;
}
////////////////////////////////////////// template.cpp ends here
Is there a way to call f with an int parameter so that the generic f<T>
is called instead of the f<intspecialisation?

f(3u) or f<unsigned>(3) would be OK?

--
Tadeusz B. Kopec (tko...@NOSPAMPLEASElife.pl)

Not really. An unsigned int is, of course, not the same as a signed
int.
Naturally, it would generate and call f(char). I want to find out if,
even though I have provided f<int>(int), there was a way to make the
compiler generate f(int) using the f<T>(int T) template. This function
would, of course, be different from the one that I provided.

As I said in my original post, I don't actually want to do this.
Neither do
I foresee a need to do it, so don't think too hard about it.
Nov 24 '07 #6
On Nov 23, 1:31 pm, "John Scheldroup" <johnscheldr...@comcast.net>
wrote:
<johnbrown...@hotmail.comwrote in messagenews:55**********************************@i 12g2000prf.googlegroups.com...
On Nov 23, 9:49 am, Matthias Buelow <m...@incubus.dewrote:
johnbrown...@hotmail.com wrote:
Is there a way to call f with an int parameter so that the generic
f<Tis called instead of the f<intspecialisation?
There is no f<T>, a template is kind of a compiler macro, the compiler
generates functions only if you instantiate a template (expand the
macro, in other words).
I understand that. Consider the output of the program that I posted:
overloaded void f(int j) called: j == 1
specialization void f<int>(int i) called: i == 2
specialization void f<int>(int i) called: i == 3
template<Tvoid f(T t) called: t == 4
Is there a way to write a call to f(5), so that the output would be:
template<Tvoid f(T t) called: t == 5
Never mind the semantic accuracy or elegance (or lack thereof!) of
what I wrote. If this is what we are aiming for, I suppose the
question
would be:
Is there a way to make the compiler generate its own instantiation,
rather than use the explicit one that I provided? When asked this way,
it would seem that the answer would be no, because if it generated its
own instantiation, it would be a duplicate symbol, because mine
already
exists.
Still, one never knows if the Powers That Be thought of some clever
reason to allow this, that mere mortals such as myself could not.
That's why I'm asking.

Now suppose you have some sort of container class called
dweeblist that is filled with dweeb objects. A pointer to the
current dweeb object is produced with member function current().
You can define a function that applies a member function
(which is passed as an argument) to all dweeb objects in dweeblist
and then call this function for dweeblist dl like this:

#include <iostream.h>

class dweeb {
public:
void spin () {cout << "spinning!" << endl; }
void dodge () {cout << "dodging!" << endl; }
void feint () {cout << "feinting!" << endl; }

};

template<int szclass dweeblist {
dweeb list [sz];
int cursor;
public:
dweeblist () : cursor(0) {}
void reset () { cursor = 0; }
dweeb * current () { return &list [cursor]; }
void next () { cursor++; }
int end () { return cursor >= sz; }
void apply (void (dweeb::* df) () ) {
reset ();
while ( !end() ) {
(current ()->*df) ();
next ();
}
}

};

void main () {
dweeblist<7dl;
dl.apply(&dweeb::spin);
dl.apply(&dweeb::dodge);
dl.apply(&dweeb::feint);

}

Typically, the kind of function you'd want to apply()
to every object in a list would be something like draw().
for CAD.

Pointers to members have fairly limited use, but they
can be quite helpful when you want to delay the selection
of a specific function until the program is running.

C++ Inside & Out
Bruce Eckel
I'm not sure how this relates to my question. The reference to
Bruce Eckel is interesting, as I am currently working through
the chapter on templates in his "Thinking in C++ Vol. 2"
Nov 24 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section...
2
by: Simon G Best | last post by:
Hello! I have a query regarding explicit specialisation of class templates which are themselves members of class templates. Here's what I want to do: template< class T > struct pink { ...
12
by: Tim Clacy | last post by:
Your expertise will be appreciated... Here's a general templatised class; all specialisations of this class should have a pointer to a specialisation of the same, templatised type: ...
7
by: Lionel B | last post by:
Greetings. The following code compiles ok and does what I'd expect it to do: ---------- START CODE ---------- // test.cpp
2
by: Stephen Starkie | last post by:
Hi, For a while I have had some problem understanding just how template specialisation works in certain cases. In abridged form my code looks like this; --MyTemplate.h-- #ifndef MyTemplateH...
8
by: Paul Roberts | last post by:
Hi, I'm hoping somebody here can help me with a simple problem of template syntax. Here's an example: template<typename T, int iclass A { static int a;
8
by: Rahul | last post by:
Hi, Is there a way to partially specialize only a member function of a template class (not the whole class). e.g. template <typename A, typename B> class Base { public:
9
by: stephen.diverdi | last post by:
Can anyone lend a hand on getting this particular template specialization working? I've been trying to compile with g++ 4.1 and VS 2005. ...
8
by: flopbucket | last post by:
Hi, I want to provide a specialization of a class for any type T that is a std::map. template<typename T> class Foo { // ... };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.