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<T void f(T t) called: t == " << t
<< std::endl;
}
template<void f<int>(int i)
{
std::cout << "specialisa tion 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<intspecialisa tion?
I don't want to do this, and I cannot think of a reason for doing it.
I'm just wondering. 6 1640 jo**********@ho tmail.com wrote:
Is there a way to call f with an int parameter so that the generic
f<Tis called instead of the f<intspecialisa tion?
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).
On Nov 23, 9:49 am, Matthias Buelow <m...@incubus.d ewrote:
johnbrown...@ho tmail.com wrote:
Is there a way to call f with an int parameter so that the generic
f<Tis called instead of the f<intspecialisa tion?
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.
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<T void f(T t) called: t == " << t
<< std::endl;
}
template<void f<int>(int i)
{
std::cout << "specialisa tion 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<intspecialisa tion?
f(3u) or f<unsigned>(3) would be OK?
--
Tadeusz B. Kopec (tk****@NOSPAMP LEASElife.pl)
Great minds run in great circles.
<jo**********@h otmail.comwrote in message news:55******** *************** ***********@i12 g2000prf.google groups.com...
On Nov 23, 9:49 am, Matthias Buelow <m...@incubus.d ewrote:
>johnbrown...@h otmail.com wrote:
Is there a way to call f with an int parameter so that the generic
f<Tis called instead of the f<intspecialisa tion?
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
On Nov 23, 12:51 pm, "Tadeusz B. Kopec" <tko...@NOSPAMP LEASElife.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<T void f(T t) called: t == " << t
<< std::endl;
}
template<void f<int>(int i)
{
std::cout << "specialisa tion 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<intspecialisa tion?
f(3u) or f<unsigned>(3) would be OK?
--
Tadeusz B. Kopec (tko...@NOSPAMP LEASElife.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.
On Nov 23, 1:31 pm, "John Scheldroup" <johnscheldr... @comcast.net>
wrote:
<johnbrown...@h otmail.comwrote in messagenews:55* *************** *************** ***@i12g2000prf .googlegroups.c om...
On Nov 23, 9:49 am, Matthias Buelow <m...@incubus.d ewrote:
johnbrown...@ho tmail.com wrote:
Is there a way to call f with an int parameter so that the generic
f<Tis called instead of the f<intspecialisa tion?
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" This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 18.3.1, 'Iseq'). I want the version for containers that he
gives, but also to provide a specialization for construction from a
pair<It,It> (eg because that is returned by equal_range()).
|
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 {
template< class U > struct floyd;
|
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:
template<typename T, const int N>
struct Base : T
{
typedef Base<T, N> Type;
|
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
|
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
#define MyTemplateH
template <class Type>
| |
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;
|
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:
|
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.
//------------------------------------------------------------------
// my regular glass
class A { };
// my templated class
|
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
{
// ...
};
|
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...
|
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...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |